【问题标题】:What requisities must WCF service have to be used as service/web reference?必须将 WCF 服务用作服务/Web 参考的哪些要求?
【发布时间】: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


    【解决方案1】:

    你可以让它几乎和你的例子一样顺利...... 因为你的话题比较宽泛,我就不详细说了,大家指点一下。

    对于您自己的类,能够将它们用作参数和/或返回类型。 (在您的情况下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;
    
    }
    

    现在你可以使用你的类了。

    调用服务:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 2012-04-29
      • 1970-01-01
      • 2011-07-19
      • 2010-11-15
      • 1970-01-01
      相关资源
      最近更新 更多