如何以相同的URL以编程方式创建WCF服务及其元数据

Igo*_*aka 7 .net wcf web-services

TL; DR
我将在同一个URL上运行所有这三件事("您已创建Web服务"页面,WSDL页面和实际Web服务),类似于在独立WebService应用程序中创建的WCF服务项目.

我正在以编程方式创建WCF端点并将其中的大部分组合在一起.最后一件事是我无法将元数据URL与服务URL相同.我知道这应该是可行的,因为您可以从Visual Studio创建类似的服务.

我可以在浏览器中浏览WSDL,我可以将其添加为Web引用,但我无法从新创建的项目中调用它.如果我删除友好页面和wsdl页面,我可以调用该服务.

下面是我正在使用的代码.

class Program
{
    private static ManualResetEvent _ResetEvent = new ManualResetEvent(false);

    static void Main(string[] args)
    {
        Console.TreatControlCAsInput = true;

        var serviceUrl = "Fibonacci.svc";
        new Thread(() =>
        {
            var baseUri = new Uri("http://ws.test.com");
            var serviceUri = new Uri(baseUri, serviceUrl);
            BasicHttpBinding binding = new BasicHttpBinding();
            using (var host = new ServiceHost(typeof(Fibonacci), new[] { baseUri }))
            {
                host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true, HttpGetUrl = new Uri(baseUri, serviceUrl) });
                host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
                host.Description.Behaviors.Find<ServiceDebugBehavior>().HttpHelpPageUrl = serviceUri;

                host.AddServiceEndpoint(typeof(IFibonacci), binding, serviceUri);

                Console.WriteLine("Started service on cotnract {0}, ready for anything", typeof(IFibonacci).FullName);

                host.Open();
                _ResetEvent.WaitOne();
            }
        }).Start();



        while (true)
        {
            var cki = Console.ReadKey(true);
            if (cki.Key == ConsoleKey.C && (cki.Modifiers & ConsoleModifiers.Control) != 0)
            {
                _ResetEvent.Set();
                break;
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Igo*_*aka 9

事实证明解决方案很简单.HttpGetUrl文档有点迟钝,基本上是为了让所有三个工作都需要创建ServiceHost带有服务的完整URL所需的相同URL ,然后才设置ServiceMetaBaseBehaviour.HttpGetEnabletrue.

相关代码如下.

var baseUri = new Uri("http://ws.test.com");
var serviceUri = new Uri(baseUri, serviceUrl);
BasicHttpBinding binding = new BasicHttpBinding();
using (var host = new ServiceHost(typeof(Fibonacci), serviceUri /*Specify full URL here*/))
{
    host.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true /*Do not specify URL at all*/});
    host.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
    host.Description.Behaviors.Find<ServiceDebugBehavior>().HttpHelpPageUrl = serviceUri;

    host.AddServiceEndpoint(typeof(IFibonacci), binding, string.Empty /*Url here can either be empty or the same one as serviceUri*/);

    Console.WriteLine("Started service on cotnract {0}, ready for anything", typeof(IFibonacci).FullName);

    host.Open();
    _ResetEvent.WaitOne();
}
Run Code Online (Sandbox Code Playgroud)