c# - IIS 7.5 中带有 REST/SOAP 端点的 WCF 4 服务

标签 c# wcf rest soap

您好,感谢您的阅读。

我正在尝试获取在 IIS 7.5 中托管的服务,该服务公开了多个端点。

我感觉问题出在我的 web.config 中,但我会在此处发布我的服务代码。没有接口(interface)文件,因为我使用的是 WCF 4 的较新功能,也没有 .svc 文件。

据我所知,所有路由都是在 Global.asax.cs 中使用 RouteTable 功能处理的。

无论如何,进入代码/配置 -

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
// NOTE: If the service is renamed, remember to update the global.asax.cs file
public class Service1
{
    // TODO: Implement the collection resource that will contain the SampleItem instances

    [WebGet(UriTemplate = "HelloWorld")]
    public string HelloWorld()
    {
        // TODO: Replace the current implementation to return a collection of SampleItem instances
        return "Hello World!";
    }
}

现在,我认为需要进行更改的配置(我不确定是否需要保留 standardEndpoints block ,但有或没有它我仍然收到错误消息。-

<services>
  <service name="AiSynthDocSvc.Service1" behaviorConfiguration="HttpGetMetadata">
    <endpoint name="rest"
              address=""
              binding="webHttpBinding"
              contract="AiSynthDocSvc.Service1"
              behaviorConfiguration="REST" />
    <endpoint name="soap"
              address="soap"
              binding="basicHttpBinding"
              contract="AiSynthDocSvc.Service1" />
    <endpoint name="mex"
              address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>    

<behaviors>
  <endpointBehaviors>
    <behavior name="REST">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="HttpGetMetadata">
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

<standardEndpoints>
  <webHttpEndpoint>
    <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
    -->
    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
  </webHttpEndpoint>
</standardEndpoints>

Global.asax.cs 文件被单独留下。

我很确定这与我的配置有关。当我尝试访问任何定义的端点时遇到的错误是 -

The endpoint at '' does not have a Binding with the None MessageVersion. 'System.ServiceModel.Description.WebHttpBehavior' is only intended for use with WebHttpBinding or similar bindings.

有人对此有任何想法吗?

谢谢,

扎卡里·卡特

最佳答案

好的,我试着复制你的东西 - 对我来说就像一个魅力:-)

  • 我使用了你们的服务等级——没有变化
  • 我在 global.asax.cs 中使用了您的 RegisterRoutes 调用

当我从 Visual Studio 中启动 Web 应用程序时,Cassini(内置 Web 服务器)出现在 http://localhost:3131/ 上 - 这对您来说可能很谨慎.

现在,我可以使用第二个浏览器窗口轻松导航到那里,而且我确实在这个 URL 上得到了一个简单的响应:

http://localhost:3131/Service1/HelloWorld
+--------------------+ 
 from Cassini
                     +--------+
                   name (first param) in ServiceRoute registration
                              +-----------+
                               from your URI template on the WebGet attribute

同样的 URL 对你有用吗??

更新:这是我的配置 - 我可以使用 REST 在浏览器中连接到 http://localhost:3131/Service1/HelloWorld,我可以连接到 http://localhost:3131/Service1/soap 使用 WCF 测试客户端进行 SOAP 调用(我的 Service1 位于 RestWebApp 命名空间中 -因此我的服务和契约(Contract)名称与您的略有不同 - 但除此之外,我相信它与您自己的配置相同):

  <system.serviceModel>
    <serviceHostingEnvironment     
        aspNetCompatibilityEnabled="true" />
    <services>
      <service name="RestWebApp.Service1" behaviorConfiguration="Meta">
        <endpoint name="rest" 
                  address=""
                  binding="webHttpBinding"
                  contract="RestWebApp.Service1"
                  behaviorConfiguration="REST" />
        <endpoint name="SOAP"
            address="soap"
            binding="basicHttpBinding"
            contract="RestWebApp.Service1" />
        <endpoint name="mex"
            address="mex"
            binding="mexHttpBinding"
            contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="REST">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="Meta">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

关于c# - IIS 7.5 中带有 REST/SOAP 端点的 WCF 4 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5651847/

相关文章:

wcf - wcf中配置属性MaxReceivedMessageSize的最大限制会影响服务性能吗?

使用 SSL 的 WCF 服务

Windows Azure REST API MediaLink

带有httpurlconnection的android api 23中的Java rest api

json - 重定向循环检测到 Dart

c# - 自定义 ModelBinder 不适用于 Nullable 类型

c# - 在 ASP.NET(C#) 中填充 GridView 时出现问题

c# - WCF 服务中的 WebOperationContext.Current null

C# - 将类的新实例添加到数组

c# - 将 zip 中的文件提取到 jar 存档中