【问题标题】:Programmatically determine endpoint for Web Service in C#在 C# 中以编程方式确定 Web 服务的端点
【发布时间】:2011-09-15 23:31:46
【问题描述】:

我正在为我制作的 Windows 服务构建一个插件(它是 C#,使用插件来完成某些功能)。这个新插件将使用 Web 服务来调用 Web 服务并获取一些信息。

很遗憾,我的 DEV、QC 和 PRODUCTION 环境的 Web 服务 URL 不同。我想让那个端点 URL 是可配置的(插件将查看数据库并获取 URL)。

究竟如何在我的代码中设置 Web 服务调用程序,以便它可以使用动态端点?

我可以添加一个服务并指向 DEV 中的现有服务 - 这构建了我的代理类 - 但是我怎样才能使它不被 URL “硬锁定” - 所以插件可以在任何环境中工作(基于它提取的数据库中的 URL)?可以这么说,我希望能够在代码中即时更改它。

【问题讨论】:

    标签: c# wcf web-services


    【解决方案1】:

    基本上你可以调用它来创建你的 WCF 服务客户端:

    MyClient = new MyWCFClient(new BasicHttpBinding("CustomBinding"), new EndpointAddress(GetEndpointFromDatabase()));
    

    其中GetEndpointFromDatabase() 返回string - 端点。

    【讨论】:

      【解决方案2】:

      我制作了一个在 LINQPad 中运行的端到端示例。这是一个完全自托管的场景,可以探索各种绑定等(对于客户端和服务器)。希望它不会过分夸大并发布整个示例,以防您以后发现任何其他方面有帮助。

      
      void Main()
      {
          MainService();
      }
      // Client
      void MainClient()
      {
          ChannelFactory cf = new ChannelFactory(new WebHttpBinding(), "http://localhost:8000");
          cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
          IService channel = cf.CreateChannel();
          Console.WriteLine(channel.GetMessage("Get"));
          Console.WriteLine(channel.PostMessage("Post"));
          Console.Read();
      }
      // Service
      void MainService()
      {
          WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:8080"));
          ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService),new WebHttpBinding(), "");
          ServiceDebugBehavior stp = host.Description.Behaviors.Find();
          stp.HttpHelpPageEnabled = false;
          stp.IncludeExceptionDetailInFaults = true;
          host.Open();
          Console.WriteLine("Service is up and running");
          Console.ReadLine();
          host.Close();    
      }
      // IService.cs
      [ServiceContract]
      public interface IService
      {
          [OperationContract]
          [WebInvoke(Method="GET", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
          string GetMessage(string inputMessage);
      
          [OperationContract]
          [WebInvoke(Method="POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
          string PostMessage(string inputMessage);
      
          [OperationContract]
          [WebInvoke(Method="POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
          System.IO.Stream PostJson(System.IO.Stream json);
      }
      // Service.cs
      public class Service : IService
      {
          public string GetMessage(string inputMessage){
              Console.WriteLine(inputMessage);
              return "Calling Get for you " + inputMessage;
          }
          public string PostMessage(string inputMessage){
              Console.WriteLine(inputMessage);
              return "Calling Post for you " + inputMessage;
          }
          public System.IO.Stream PostJson (System.IO.Stream json) {
              Console.WriteLine(new System.IO.StreamReader(json).ReadToEnd());
              return json;
          }
      }
      

      【讨论】:

        【解决方案3】:

        你不能把 URI 放在 .config 文件中吗?您可以在调试或发布时更改 URI,方法是在 .debug.config 和 .release.config 中使用不同的 URI。

        【讨论】:

        • 是的,但这意味着部署中有一个额外的步骤。通过这种方式,我们可以告诉工程“将这个 DLL 放到这个文件夹中”,并减少“风险”,减少步骤。
        【解决方案4】:

        只需设置url点

        TheWebservice.Url = TheUrlFromTheDatabase;
        

        【讨论】:

        • 没有 .URL 属性,这是一个 WCF 服务引用。
        猜你喜欢
        • 1970-01-01
        • 2019-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-03
        • 1970-01-01
        • 2018-09-01
        • 1970-01-01
        相关资源
        最近更新 更多