WCF Self Host Service - Endpoints in C#

前端 未结 3 2001
没有蜡笔的小新
没有蜡笔的小新 2020-12-18 01:46

My first few attempts at creating a self hosted service. Trying to make something up which will accept a query string and return some text but have have a few issues:

<
3条回答
  •  醉梦人生
    2020-12-18 02:25

    For your question about default endpoints not being added:

    • first of all, that's a WCF 4 feature - it will work on .NET 4 only
    • next, the default endpoints are only added to your service host if you have no explicit endpoints defined in config, and if you do not add endpoints yourself in code! By adding those two endpoints in code, you're taking charge and the WCF 4 runtime will not fiddle with your config

    Check out this MSDN library article for more information on What's new in WCF 4 for developers. It shows, among other things, how to use default endpoints - you basically define a base address for your service and open the ServiceHost - that's all!

    string baseaddr = "http://localhost:8080/HelloWorldService/";
    Uri baseAddress = new Uri(baseaddr);
    
    // Create the ServiceHost.
    using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
    {
       //for some reason a default endpoint does not get created here
       host.Open();
    
       // here, you should now have one endpoint for each contract and binding
    }
    

    You can also add the default endpoints explicitly, in code, if you wish to do so. So if you need to add your own endpoints, but then you want to add the system default endpoints, you can use:

    // define and add your own endpoints here
    
    // Create the ServiceHost.
    using (ServiceHost host = new ServiceHost(typeof(HelloWorldService), baseAddress))
    {
       // add all the system default endpoints to your host
       host.AddDefaultEndpoints();
    
       //for some reason a default endpoint does not get created here
       host.Open();
    
       // here, you should now have your own endpoints, plus 
       // one endpoint for each contract and binding
    }
    

    I also fonud this blog post here to be quite illuminating - Christopher's blog is full of good and very helpful WCF posts - highly recommended.

提交回复
热议问题