我在 Visual Studio 2010 中制作了一个 WCF 服务应用程序。除了这个之外,每种方法都有效:
public List<Friend> SendFriendList(int id)
{
FyfDataContext db = new FyfDataContext();
List<Friend> friends = db.Friends.Where(P => P.PK_ID == id).ToList();
return friends;
}
当我在谷歌浏览器中测试它时,它实际上完美地接收了数据,但没有显示它。
转折点是:在出现这个问题之前,我在 Visual Studio 2012 中制作了整个 WCF 服务应用程序。它在那里完美运行 - 但由于其他一些问题,我不得不回到 2010 年。
我的 IService.cs 的片段:
public interface IService2
{
[OperationContract]
[WebGet(UriTemplate = "/Friendlist?id={id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<Friend> SendFriendList(int id);
[OperationContract]
[WebGet(UriTemplate = "/Friendloc?id={id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Friendloc SendFriendLoc(int id);
}
Friendloc 方法有效 - 但与所有其他方法一样,它返回一个对象,而不是列表。
我怀疑问题出在 web.config 上——但我就是不知道是什么问题。我确实意识到 VS12 制作了 4.5 框架,而 VS10 制作了 4.0。
这是Visual Studio 2010 Web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="FyfConnectionString" connectionString="Data Source=.;AttachDbFilename=|DataDirectory|\Fyf.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name ="servicebehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restbehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name ="Fyf.Service1"
behaviorConfiguration ="servicebehavior" >
<endpoint name ="SOAPEndPoint"
contract ="Fyf.IService1"
binding ="basicHttpBinding"
address ="soap" />
<endpoint name ="RESTEndPoint"
contract ="Fyf.IService2"
binding ="webHttpBinding"
address ="rest"
behaviorConfiguration ="restbehavior"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
这是来自 Visual Studio 2012 的 Web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="FyfdbConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Fyfdb.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<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 name ="servicebehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restbehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name ="Fyf.Service1"
behaviorConfiguration ="servicebehavior" >
<endpoint name ="SOAPEndPoint"
contract ="Fyf.IService1"
binding ="basicHttpBinding"
address ="soap" />
<endpoint name ="RESTEndPoint"
contract ="Fyf.IService2"
binding ="webHttpBinding"
address ="rest"
behaviorConfiguration ="restbehavior"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
</service>
</services>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
我尝试将一些元素从 VS12 版本复制到我的 VS10;例如 aspNetCompatibilityEnabled 为“true”并将 directoryBrowse 放入等。但没有运气。
如果我错过了一些非常简单的东西,我很抱歉。这是我的第一个 WCF 服务。任何帮助深表感谢!