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.
问题:
回答1:
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!
回答2:
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>
回答3:
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.