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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2142712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:51:58  来源:igfitidea点击:

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

c#.netwcfconfiguration

提问by David Basarab

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");

采纳答案by marc_s

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

通常,在使用 WCF 执行 REST 时,您可以使用<webHttp>配置中的行为,也可以使用WebServiceHost类(而不是“普通香草” ServiceHost)。使用WebServiceHost包括所有必要的调整和零碎的部分来使 REST 工作 - 不再需要 webHttp 行为。

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(in System.ServiceModel.Web),它专门将服务作为 REST 托管。这可能是也可能不是您正在寻找的:

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 Behaviorscollection (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 :-)

我希望这两种方式都能让你达到你的目标!(或者至少让你更亲近:-)