简体   繁体   English

以编程方式确定C#中Web服务的终结点

[英]Programmatically determine endpoint for Web Service in C#

I'm building a plugin to a windows service I've made (it's C#, uses plugins to do certain functionality). 我正在为自己制作的Windows服务构建插件(它是C#,使用插件来执行某些功能)。 This new plugin will be using Web Services to make calls to a web-service and get some information. 这个新插件将使用Web服务来调用Web服务并获取一些信息。

Unfortunately, the web-service URL is different for my DEV, QC, and PRODUCTION environments. 不幸的是,对于我的DEV,QC和PRODUCTION环境,Web服务URL是不同的。 I'd like to make that end-point URL be configurable (the plugin will look into the database and get the URL). 我想使该端点URL可配置(插件将查看数据库并获取URL)。

How, exactly, do I set up a web-service caller in my code so that it can use a dynamic endpoint? 究竟如何在代码中设置一个Web服务调用程序,使其可以使用动态端点?

I can add a service and point to the existing one in DEV - and that builds up my proxy class - but how can I make it so it's not "hard locked" with the URL - so the plugin works in any environment (based on the URL in the database it pulls out)? 我可以添加服务并指向DEV中现有的服务-这样就建立了我的代理类-但是我该如何做到这一点,以便它不会被URL“硬锁”-因此该插件可以在任何环境下工作(基于它拉出的数据库中的URL)? I'd like to be able to change that on the fly in the code, so to speak. 可以这么说,我希望能够即时更改代码中的内容。

Basically you can call this to create your WCF Service client: 基本上,您可以调用此命令来创建WCF服务客户端:

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

Where GetEndpointFromDatabase() returns a string - the endpoint. 其中GetEndpointFromDatabase()返回一个string -端点。

I made an end to end sample which runs in LINQPad. 我做了一个端到端的示例,该示例在LINQPad中运行。 This is for a completely self-hosted scenario, and enables exploring various bindings, etc (for both the client and the server). 这是针对完全自托管的方案,并允许探索各种绑定等(对于客户端和服务器)。 Hope it's not over the top and posted the entire sample in case you find any of the other aspects helpful later on. 希望它不会放在顶部,并发布整个示例,以防日后发现其他方面的帮助。


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;
    }
}

Can't you just put the URI inside the .config file? 您不能只将URI放在.config文件中吗? You can just change the URI when it's debug or release by having different URI inside .debug.config and .release.config. 您可以通过在.debug.config和.release.config中使用不同的URI来在调试或发布时更改URI。

只需设置网址点

TheWebservice.Url = TheUrlFromTheDatabase;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM