【发布时间】:2015-08-13 08:59:16
【问题描述】:
这些天我正在学习 WCF,可能不知道从哪里开始。我想创建 WCF REST 服务,它可以通过 HTTP 请求(GET、PUT ...)访问。同时我希望能够将此服务添加为服务引用或 Web 引用,并将它们作为普通方法在 Web 应用程序客户端中使用。这个问题非常广泛,因此我将不胜感激任何提示或指导。
此时,我有功能性服务并在我的主机上运行它们。我可以添加服务参考和网络参考。正如我所认识到的,服务引用更适合新代码,因为它使用 WCF 通信,因此它包含所有以前的通信渠道。当我添加这些引用时,我可以使用对 GetSimpleDataService 的引用,但不能使用它的方法。当我尝试添加这些方法作为参考时,会注意到元数据存在问题。
WCF 接口:
[ServiceContract]
public interface IGetSimpleDataService
{
[OperationContract]
[WebGet(UriTemplate = "User/{ID}")]
User GetUser(string ID);
[OperationContract]
[ScriptMethod(UseHttpGet = true)]
User GetUserByMethod(string ID);
[OperationContract]
[WebGet]
string ActivationTest();
[OperationContract]
[WebMethod]
string WebMethodTest();
}
Web.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.serviceModel>
<services>
<service name="StoryHubWCFApp.TestStudentService" behaviorConfiguration="serviceBehavior">
<endpoint address=""
binding="webHttpBinding"
contract="StoryHubWCFApp.ITestStudentService"
behaviorConfiguration="web"
/>
</service>
<service name="StoryHubWCFApp.GetSimpleDataService" behaviorConfiguration="serviceBehavior">
<endpoint address=""
binding="webHttpBinding"
contract="StoryHubWCFApp.IGetSimpleDataService"
behaviorConfiguration="web"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</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>
</configuration>
现在我可以通过 GET 请求获取数据,但我希望能够像这样使用带有 Web/Service 引用的服务。:
string s = MyServices.ActivationTest();
我假设使用这样的方法,它返回或接受除 int 和 string 以外的值,我应该有 [DataContracts]?我也明白了,我必须使用[WebMethod]或[ScriptMethod],但我目前没有成功。
感谢您的任何更正。
【问题讨论】:
标签: c# .net web-services wcf service