如何调用WCF URL?

时间:2016-05-11 06:05:02

标签: c# asp.net-mvc wcf wcf-rest

我创建了一个简单的测试服务,如下所示。我想按照URI模板中的定义将URL提供给我的客户端。 我在IIS中托管了该服务。 如何为以下服务创建网址??????

wsdl文档按预期显示。 我希望网址与URITemplate匹配。

请建议

IService1.cs

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = "?value={value}")]
    string GetData(string value);


    // TODO: Add your service operations here
}

Service1.cs

public class Service1 : IService1
{

    #region IService1 Members

    public string GetData(string value)
    {
        return "Hello " + value;
    }

    #endregion
}

的web.config

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
   <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
      </customHeaders>
    </httpProtocol>
        <handlers accessPolicy="Read, Execute, Script" />
  </system.webServer>
  <system.serviceModel>
    <services>
      <service name="Check.Service1" behaviorConfiguration="mexBehavior">
        <endpoint address="" binding="webHttpBinding" contract="Check.IService1" behaviorConfiguration="restfulBehavior"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/Check" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="restfulBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false"></serviceHostingEnvironment>
  </system.serviceModel>
</configuration>

1 个答案:

答案 0 :(得分:1)

我刚刚使用您的代码,使用以下网址正常工作:

http://[HostAddress]/service1.svc/?value=1

我在回复中得到了这个:

{
    GetDataResult: "Hello 1"
}

检查它是否适合您。