必须将 WCF 服务用作 service/web 参考的哪些要求?

What requisities must WCF service have to be used as service/web reference?

最近在学习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();

我假设使用这样的方法,returns 或采用除 int 和 string 以外的值我应该有 [DataContracts]?我也明白了,我必须使用[WebMethod]或[ScriptMethod],但到目前为止我没有成功。

感谢您的任何更正。

您几乎可以像您的示例所示那样顺利... 由于你的话题比较广泛,我就不详细说了,只是指点一下。

对于您自己的 classes,能够将它们用作参数 and/or return 类型。 (在您的情况下 User)您必须定义要序列化的内容和方式。你可以在这里阅读:

https://msdn.microsoft.com/en-us/library/ms733127(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/ms733811(v=vs.110).aspx

示例:

[DataContract]
public class User
{
    // This member is serialized.
    [DataMember]
    string FullName;

    // This is not serialized because the DataMemberAttribute 
    // has not been applied.
    private string MailingAddress;

}

现在您可以使用 class。

调用服务:

您可以添加服务引用:https://msdn.microsoft.com/en-us/library/bb386386.aspx(这将是您的服务的生成代理)

或者您可以使用 ChannelFactory:https://msdn.microsoft.com/en-us/library/ms734681(v=vs.110).aspx (有了这个,您可以完全控制代码,但可能需要做更多设置,即端点。)