如何以编程方式创建相同的URL WCF服务及其元数据 [英] How to programatically create a WCF service and its metadata on the same URL

查看:175
本文介绍了如何以编程方式创建相同的URL WCF服务及其元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR
我会跑三个东西放在同一个URL(您创建了一个Web服务页面,WSDL页面和实际的Web服务),类似于一个独立的web服务应用程序创建一个WCF服务项目。

我的工作编程创建WCF终端,并获得大部分在一起。的最后一件事是,我不能得到的元数据的URL是相同的服务URL。我知道这应该是可能的,你可以像在Visual Studio中创建的服务。

会发生什么事是我可以在浏览器中浏览WSDL,我可以将其添加为一个Web引用,但我不能从新建项目调用它。如果我删除这两个友好的页面和WSDL的网页,我可以调用服务。

下面是我使用的code。

 类节目
{
    私有静态ManualResetEvent的_ResetEvent =新的ManualResetEvent(假);

    静态无效的主要(字串[] args)
    {
        Console.TreatControlCAsInput =真;

        VAR的serviceUrl =Fibonacci.svc;
        新的Thread(()=>
        {
            VAR基本URI =新的URI(http://ws.test.com);
            VAR serviceUri分别=新的URI(基本URI,的serviceUrl);
            basicHttpBinding的约束力=新basicHttpBinding的();
            使用(VAR主机=新的ServiceHost(typeof运算(斐波那契),新的[] {}基本URI))
            {
                host.Description.Behaviors.Add(新的ServiceMetadataBehavior {HttpGetEnabled = TRUE,HttpGetUrl =新的URI(基本URI,的serviceUrl)});
                host.Description.Behaviors.Find< ServiceDebugBehavior>()IncludeExceptionDetailInFaults =真。
                host.Description.Behaviors.Find< ServiceDebugBehavior>()HttpHelpPageUrl = serviceUri分别;

                host.AddServiceEndpoint(typeof运算(IFibonacci),装订,serviceUri分别);

                Console.WriteLine(关于cotnract启动服务{0},做好了一切准备的typeof(IFibonacci).FullName);

                host.Open();
                _ResetEvent.WaitOne();
            }
        })。开始();



        而(真)
        {
            VAR CKI = Console.ReadKey(真正的);
            如果(cki.Key == ConsoleKey.C&安培;及(cki.Modifiers&安培;!ConsoleM​​odifiers.Control)= 0)
            {
                _ResetEvent.Set();
                打破;
            }
        }

    }
}
 

解决方案

原来,解决方法很简单。该<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.servicemetadatabehavior.httpgetenabled.aspx">documentation对于HttpGetUrl 是稍钝,基本上都是为了得到这三个工作过,你需要创建的ServiceHost 完整的URL <同一网址/ STRONG>服务,然后只设置 ServiceMetaBaseBehaviour.HttpGetEnable

相关code是如下。

  VAR基本URI =新的URI(http://ws.test.com);
VAR serviceUri分别=新的URI(基本URI,的serviceUrl);
basicHttpBinding的约束力=新basicHttpBinding的();
使用(VAR主机=新的ServiceHost(typeof运算(斐波那契),serviceUri分别/ *在此处指定完整的URL * /))
{
    host.Description.Behaviors.Add(新的ServiceMetadataBehavior {HttpGetEnabled = TRUE / ​​都* /不要指定URL});
    host.Description.Behaviors.Find&LT; ServiceDebugBehavior&GT;()IncludeExceptionDetailInFaults =真。
    host.Description.Behaviors.Find&LT; ServiceDebugBehavior&GT;()HttpHelpPageUrl = serviceUri分别;

    host.AddServiceEndpoint(typeof运算(IFibonacci),结合,的String.Empty / *网址这里可以为空或者同一个作为serviceUri分别* /);

    Console.WriteLine(关于cotnract启动服务{0},做好了一切准备的typeof(IFibonacci).FullName);

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

TL;DR
I would to run all three things("You have created a web service" page, WSDL page and actual web service) on the same URL, similarly to a WCF service project created in a standalone WebService application.

I am working on creating WCF endpoints programatically and got most of it together. The last thing is that I can't get the metadata URL to be the same as the service URL. I know this should be possible as you can create services like that from Visual Studio.

What happens is I can browse WSDL in the browser, I can add it as a web reference but I can't invoke it from the newly created project. If I remove both friendly page and wsdl pages, i can invoke the service.

Below is the code that I am using.

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

    }
}

解决方案

Turns out the solution is simple. The documentation for HttpGetUrl is slightly obtuse, basically in order to get all three to work off the same URL you need to create the ServiceHost with the full URL of the service and then only set ServiceMetaBaseBehaviour.HttpGetEnable to true.

Relevant code is below.

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

这篇关于如何以编程方式创建相同的URL WCF服务及其元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆