我如何创建代码,而不是配置WCF EndPointBehaviors? [英] How do I create WCF EndPointBehaviors in Code rather than the configuration?

查看:694
本文介绍了我如何创建代码,而不是配置WCF EndPointBehaviors?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的XML配置



 < system.serviceModel> 
<服务和GT;
<服务名称=MyService.MyServiceRESTbehaviorConfiguration =MyServiceTypeBehaviors>
<主机>
< baseAddresses>
<添加baseAddress =HTTP://本地主机:1234 /为MyService / XML/>
< / baseAddresses>
< /主机>
<端点地址=绑定=的WebHttpBindingbehaviorConfiguration =xmlBehavior合同=MyService.IMyService/>
< /服务>
< /服务>
<&行为GT;
< serviceBehaviors>
<行为NAME =MyServiceTypeBehaviors>
< serviceMetadata httpGetEnabled =真/>
< serviceDebug includeExceptionDetailInFaults =真/>
< /行为>
< / serviceBehaviors>
< endpointBehaviors>
<行为NAME =xmlBehavior>
< webHttp />
< /行为>
< / endpointBehaviors>
< /行为>
< /system.serviceModel>



我想在C#代码来实现,而不是使用的配置。



我想不出谁做端点webHttp揭露这项服务作为REST服务。

  ServiceHost的ServiceHost的=新的ServiceHost(singletonInstance,HTTP://本地主机:1234 /为MyService / XML); 

//创建元行为
的ServiceMetadataBehavior行为=新的ServiceMetadataBehavior();
behavior.HttpGetEnabled = TRUE;

serviceHost.Description.Behaviors.Add(行为);

结合mexBinding = MetadataExchangeBindings.CreateMexHttpBinding();

serviceHost.AddServiceEndpoint(typeof运算(IMetadataExchange接口),mexBinding,MEX);

WsHttpBinding的httpBinding =新的WSHttpBinding(SecurityMode.None);

serviceHost.AddServiceEndpoint(typeof运算(MyService.IMyService),httpBinding,休息);


解决方案

通常情况下,与WCF做休息的时候,您可以使用< webHttp> 在配置行为,也可以使用 WebServiceHost 类(而不是普通的香草的ServiceHost )。使用 WebServiceHost 包括所有必要的调整和点点滴滴,使REST的东西的工作 - 不需要更多webHttp行为



当然,这意味着,你需要一个单独的 WebServiceHost (在 System.ServiceModel.Web ),这承载服务为REST独占。这可能是也可能不是你要找的内容:

  WebServiceHost webServiceHost = 
新WebServiceHost(singletonInstance HTTP://本地主机:1234 /为MyService / XML);

的WebHttpBinding webBinding =新的WebHttpBinding();
webServiceHost.AddServiceEndpoint(typeof运算(MyService.IMyService),webBinding,休息);



你有另一种选择是一个服务端点添加到您的常规服务主机,只需配置在该端点Web的http行为 - 端点(和服务)的行为只是普通的.NET类的,毕竟,你可以实例,并添加到相应的行为集(上服务或单个端点):

 的WebHttpBinding restBinding =新的WebHttpBinding(); 

ServiceEndpoint restSEP =
serviceHost.AddServiceEndpoint(typeof运算(MyService.IMyService),
restBinding,休息);
restSEP.Behaviors.Add(新WebHttpBehavior());



这两种方式应该带你到你的目标,我希望! (或者至少让你更接近: - )


I have the following Xml Configuration

<system.serviceModel>
    <services>
         <service name="MyService.MyServiceREST" behaviorConfiguration="MyServiceTypeBehaviors">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:1234/MyService/xml"/>
                </baseAddresses>
            </host>
            <endpoint address="" binding="webHttpBinding" behaviorConfiguration="xmlBehavior" contract="MyService.IMyService" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceTypeBehaviors" >
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="True"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="xmlBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

I want to implement in C# code rather than using the config.

I cannot figure out who to do the EndPoint with webHttp to expose this service as a REST service.

ServiceHost serviceHost = new ServiceHost(singletonInstance, "http://localhost:1234/MyService/xml");

// Create Meta Behavior
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = true;

serviceHost.Description.Behaviors.Add(behavior);

Binding mexBinding = MetadataExchangeBindings.CreateMexHttpBinding();

serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, "mex");

WSHttpBinding httpBinding = new WSHttpBinding(SecurityMode.None);

serviceHost.AddServiceEndpoint(typeof(MyService.IMyService), httpBinding, "rest");

解决方案

Typically, when doing REST with WCF, you can either use the <webHttp> behavior in config, or you can use the WebServiceHost class (instead of the "plain vanilla" ServiceHost). Using the WebServiceHost includes all the necessary tweaks and bits and pieces to make the REST stuff work - no more webHttp behavior needed.

Of course, this means, you need a separate WebServiceHost (in System.ServiceModel.Web), which hosts a service as REST exclusively. This may or may not be what you're looking for:

WebServiceHost webServiceHost = 
    new WebServiceHost(singletonInstance, "http://localhost:1234/MyService/xml");

WebHttpBinding webBinding = new WebHttpBinding();
webServiceHost.AddServiceEndpoint(typeof(MyService.IMyService), webBinding, "rest");

The other option you have is the add a service endpoint to your regular service host, and just configure the web http behavior on that endpoint - endpoint (and service) behaviors are just regular .NET classes, after all, which you can instantiate, and add to the appropriate Behaviors collection (on the service or the individual endpoint):

WebHttpBinding restBinding = new WebHttpBinding();

ServiceEndpoint restSEP = 
    serviceHost.AddServiceEndpoint(typeof(MyService.IMyService), 
                                   restBinding, "rest");
restSEP.Behaviors.Add(new WebHttpBehavior());

Both ways should bring you to your goal, I hope! (or at least get your closer :-)

这篇关于我如何创建代码,而不是配置WCF EndPointBehaviors?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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