是否可以在运行时更改 UriTemplate

发布于 2024-12-07 05:55:43 字数 373 浏览 0 评论 0原文

我有以下 WebInvoke 属性:

        [OperationContract]
        [WebInvoke(
        Method         = "POST",
        UriTemplate    = "",
        BodyStyle      = WebMessageBodyStyle.Bare,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat  = WebMessageFormat.Json)]

我希望根据运行时值设置 UriTemplate 值。有没有办法在运行时在服务实现中设置UriTemplate?

I have the following WebInvoke Attribute:

        [OperationContract]
        [WebInvoke(
        Method         = "POST",
        UriTemplate    = "",
        BodyStyle      = WebMessageBodyStyle.Bare,
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat  = WebMessageFormat.Json)]

I would like the UriTemplate value to be set based on a runtime value. Is there any way to set the UriTemplate in the service implementation at runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒尘 2024-12-14 05:55:43

是的,如果您使用添加到 WebHttpBehavior 之前的端点行为,您就可以做到这一点。此行为可以更改 WebGetAttribute/WebInvokeAttribute 的属性。下面的代码显示了更改 [WebGet]UriTemplate 属性的行为示例,但它也适用于 [WebInvoke].

public class StackOverflow_7590279
{
    [ServiceContract]
    public class Service
    {
        [WebGet(UriTemplate = "/Add?x={x}&y={y}", ResponseFormat = WebMessageFormat.Json)]
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
    public class MyBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            foreach (OperationDescription od in endpoint.Contract.Operations)
            {
                if (od.Name == "Add")
                {
                    WebGetAttribute wga = od.Behaviors.Find<WebGetAttribute>();
                    if (wga != null)
                    {
                        wga.UriTemplate = "/Add?first={x}&second={y}";
                    }
                }
            }
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        var endpoint = host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "");

        // This has to go BEFORE WebHttpBehavior
        endpoint.Behaviors.Add(new MyBehavior());

        endpoint.Behaviors.Add(new WebHttpBehavior());

        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        Console.WriteLine("Using the original template (values won't be received)");
        Console.WriteLine(c.DownloadString(baseAddress + "/Add?x=45&y=67"));

        c = new WebClient();
        Console.WriteLine("Using the modified template (will work out fine)");
        Console.WriteLine(c.DownloadString(baseAddress + "/Add?first=45&second=67"));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

Yes, you can do that, if you use an endpoint behavior which is added before the WebHttpBehavior. This behavior can change the properties of the WebGetAttribute/WebInvokeAttribute. The code below shows an example of a behavior which changes the UriTemplate property of a [WebGet], but it would work just as well for [WebInvoke].

public class StackOverflow_7590279
{
    [ServiceContract]
    public class Service
    {
        [WebGet(UriTemplate = "/Add?x={x}&y={y}", ResponseFormat = WebMessageFormat.Json)]
        public int Add(int x, int y)
        {
            return x + y;
        }
    }
    public class MyBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            foreach (OperationDescription od in endpoint.Contract.Operations)
            {
                if (od.Name == "Add")
                {
                    WebGetAttribute wga = od.Behaviors.Find<WebGetAttribute>();
                    if (wga != null)
                    {
                        wga.UriTemplate = "/Add?first={x}&second={y}";
                    }
                }
            }
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        var endpoint = host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "");

        // This has to go BEFORE WebHttpBehavior
        endpoint.Behaviors.Add(new MyBehavior());

        endpoint.Behaviors.Add(new WebHttpBehavior());

        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        Console.WriteLine("Using the original template (values won't be received)");
        Console.WriteLine(c.DownloadString(baseAddress + "/Add?x=45&y=67"));

        c = new WebClient();
        Console.WriteLine("Using the modified template (will work out fine)");
        Console.WriteLine(c.DownloadString(baseAddress + "/Add?first=45&second=67"));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文