Bare Minimum Configuration for RESTful WCF

2019-04-29 04:37发布

What is the bare minimum I need to put in web.config to get WCF working with REST? I have annotated my methods with [WebGet], but they are not getting the message.

3条回答
Rolldiameter
2楼-- · 2019-04-29 05:05

You need to ensure that you have an address for your service host e.g

<services>
      <service name="SomeLib.SomeService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/somebase"/>
          </baseAddresses>
        </host>
<!-- And one EndPoint **basicHttpBinding** WILL WORK !!! -->

        <endpoint 
                   address="basic"
                   binding="basicHttpBinding"
                   contract="SomeLib.SomeContract"/>
</service>
</services>

So now, if you are self hosting via a console app for e.g...you can invoke your host via:

WebChannelFactory<IServiceContract> factory =
        new WebChannelFactory<IServiceContract>(
            new Uri("http://localhost:8080/somebase"));

When the console app starts up, the address will be browsable even if its self hosted and you should be able to invoke your actions based on your webget uri templates.

This minimum config will let you invoke WCF RestFULLY via selfhosting. If you're hosting in IIS it would essentially work the same way, except the svc file replaces our custom host.

查看更多
Root(大扎)
3楼-- · 2019-04-29 05:09

I discovered that you can add the following to the ServiceHost directive in the *.svc file, and it will automatically setup WebHttpBinding and WebHttpBehavior for you:

Factory="System.ServiceModel.Activation.WebServiceHostFactory"

Note that the namespace is a little different from what is mentioned elsewhere on the web (such as in this MSDN article).

After doing this, I was able to delete the entire section from web.config and everything still worked!

查看更多
来,给爷笑一个
4楼-- · 2019-04-29 05:28

Ensure that you use a webHttpBinding on your endpoint (and not an httpBinding or wsHttpBinding). Here's my endpoint config...

    <endpoint address="" binding="webHttpBinding" bindingConfiguration=""
      contract="WcfCore.ICustomer">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
查看更多
登录 后发表回答