简体   繁体   English

如何在代码而不是配置中创建WCF EndPointBehaviors?

[英]How do I create WCF EndPointBehaviors in Code rather than the configuration?

I have the following Xml Configuration 我有以下Xml配置

<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. 我想用C#代码实现而不是使用配置。

I cannot figure out who to do the EndPoint with webHttp to expose this service as a REST service. 我无法弄清楚使用webHttp将EndPoint作为REST服务公开的人。

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 ). 通常,在使用WCF执行REST时,您可以在config中使用<webHttp>行为,也可以使用WebServiceHost类(而不是“普通的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. 使用WebServiceHost包含所有必要的调整和零碎,以使REST工作正常 - 不再需要webHttp行为。

Of course, this means, you need a separate WebServiceHost (in System.ServiceModel.Web ), which hosts a service as REST exclusively. 当然,这意味着,您需要一个单独的WebServiceHost (在System.ServiceModel.Web ),它将服务作为REST专门托管。 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): 您拥有的另一个选项是将服务端点添加到常规服务主机,并在该端点上配置Web http行为 - 端点(和服务)行为只是常规.NET类,毕竟,您可以实例化,以及添加到相应的Behaviors集合(在服务或单个端点上):

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 :-) (或者至少让你离得更近:-)

暂无
暂无

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

相关问题 使用ClientBase调用WCF服务时如何设置EndpointBehaviors名称 - How to set endpointBehaviors name when calling wcf service using ClientBase 如何使用SQL而不是.resx文件进行本地化? - How do I use SQL for localization rather than .resx files? 如何获得完整的IP而不是:: 1? - How do I get the full IP rather than ::1? 如何直接在 WCF Response 对象中返回多个字段,而不是包装在另一个对象中? - How can i return several fields directly in a WCF Response object, rather than wrapped in another object? 有没有办法在代码中而不是在配置文件中设置配置运行时选项? - Is there a way to set configuration runtime options in code rather than in the config file? XML 配置,如何读取包含值列表/数组而不是单个值的配置文件 - XML Config, How can I read a configuration file with a list/array of values rather than a single value 如何更改此代码以获取 Cs 的最长连续序列,而不是任何字符的最长连续序列 - How do I change this code to get the longest continuous sequence of Cs rather than the longest continuous sequence of any character 在内部使用WCF配置-如何在内部和外部使用WCF服务? - Using WCF Configuration Internally - How do I use my WCF services internally and externally? 如何定义将被wcf忽略的映射代码的第一个成员 - How do I define a mapped code first member that will be ignored by wcf 如何更改WCF的默认配置? - How can I change the default configuration for WCF?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM