开发者

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

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.AddServiceEndpoi开发者_开发问答nt(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 :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜