WCF没有按预期返回字符串

时间:2014-05-27 17:35:00

标签: c# json wcf

我设计了这个界面和实现。

[ServiceContract]
public interface IGeneral
{
  [OperationContract]
  [WebInvoke(
    Method = "GET",
    RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    UriTemplate = "Bamse")]
  String Ping(String input);
}

public class General : IGeneral
{
  public String Ping(String input)
  {
    return
      "pong @" + DateTime.Now
      + " with " + input;
  }
}

返回WSDL文件并列出方法。但是,当通过下面的URL导航到它时,我在屏幕上什么都没有。我确定我忘记了一些事情,但我无法弄清楚是什么。

  

http://.../ general.svc / ping / hazaa

我注意到使用C#代码访问服务并生成的客户端会产生正确的结果。所以,我的结论是,在暴露服务时,我做了一些不那么聪明(或忘了做必要的事情)的事情。不能看到什么,但是......

<appSettings>
  <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata
          httpGetEnabled="true"
          httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>    
  <serviceHostingEnvironment 
    aspNetCompatibilityEnabled="true" 
    multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
  <directoryBrowse enabled="true"/>
</system.webServer>

2 个答案:

答案 0 :(得分:2)

问题在于您的UriTemplate。

它应该是这样的:

UriTemplate = "Ping/{input}"

然后你会这样访问它:

http://localhost/general.svc/Ping/someinputhere

修改

如果你想维护当前的UriTemplate,你仍然需要为你的输入参数添加一个slug,如下所示:

UriTemplate = "Bamse/{input}"

然后,再次访问资源,URL将是:

http://localhost/general.svc/Bamse/someinputhere

答案 1 :(得分:0)

我在配置文件中看不到<services>部分。如果要公开服务,则需要定义服务端点的地址,绑定和合同,如下所示:

 <services>      
      <service name="Services.General">            
        <endpoint address="http://..general.svc" binding="basicHttpBinding" contract="Services.IGeneral"/>        
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>      
      </service>
    </services>