RESTful WCF的裸最小配置

时间:2021-10-31 19:35:29

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.

我需要在web.config中放置什么才能让WCF与REST一起工作?我用[WebGet]注释了我的方法,但他们没有收到消息。

3 个解决方案

#1


6  

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:

我发现您可以将以下内容添加到* .svc文件中的ServiceHost指令中,它将自动为您设置WebHttpBinding和WebHttpBehavior:

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).

请注意,命名空间与Web上其他地方提到的命名空间略有不同(例如在此MSDN文章中)。

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

这样做后,我能够从web.config删除整个部分,一切仍然有效!

#2


2  

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

确保在端点上使用webHttpBinding(而不是httpBinding或wsHttpBinding)。这是我的端点配置...

    <endpoint address="" binding="webHttpBinding" bindingConfiguration=""
      contract="WcfCore.ICustomer">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>

#3


1  

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.

当控制台应用程序启动时,即使其自托管也可以浏览该地址,并且您应该能够根据您的webget uri模板调用您的操作。

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.

此最小配置将允许您通过自托管调用WCF RestFULLY。如果您在IIS中托管它将基本上以相同的方式工作,除了svc文件替换我们的自定义主机。

#1


6  

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:

我发现您可以将以下内容添加到* .svc文件中的ServiceHost指令中,它将自动为您设置WebHttpBinding和WebHttpBehavior:

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).

请注意,命名空间与Web上其他地方提到的命名空间略有不同(例如在此MSDN文章中)。

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

这样做后,我能够从web.config删除整个部分,一切仍然有效!

#2


2  

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

确保在端点上使用webHttpBinding(而不是httpBinding或wsHttpBinding)。这是我的端点配置...

    <endpoint address="" binding="webHttpBinding" bindingConfiguration=""
      contract="WcfCore.ICustomer">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>

#3


1  

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.

当控制台应用程序启动时,即使其自托管也可以浏览该地址,并且您应该能够根据您的webget uri模板调用您的操作。

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.

此最小配置将允许您通过自托管调用WCF RestFULLY。如果您在IIS中托管它将基本上以相同的方式工作,除了svc文件替换我们的自定义主机。