简体   繁体   English

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

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

I am learning WCF these days and probably don't know where to start. 这些天我正在学习WCF,可能不知道从哪里开始。 I want to create WCF REST Service, which will be accesible through HTTP requests (GET, PUT...). 我想创建WCF REST服务,该服务可通过HTTP请求(GET,PUT ...)访问。 And at same time I want to be able to add this service as service reference or web reference and use them in the Web Application client as ordinary method. 同时,我希望能够将此服务添加为服务参考或Web参考,并以普通方法在Web应用程序客户端中使用它们。 This issue is quite wide so I will by grateful for any hint or direction. 这个问题涉及面很广,因此我将不胜感激任何提示或指示。

At this time, I have functional services and run them on my hosting. 目前,我具有功能服务,并在主机上运行它们。 I can add Service Reference and Web Reference. 我可以添加服务参考和Web参考。 Service reference is better for new code, as I reckognized, because it use WCF communication and thus it contains all former communication channels. 正如我所认识到的那样,服务引用更适合新代码,因为它使用WCF通信,因此包含所有以前的通信通道。 When I add these references, I can use reference to GetSimpleDataService, but non of its methods. 添加这些引用时,可以使用对GetSimpleDataService的引用,但不能使用其方法。 When I try to add these methods as reference, problem with metadata is noted. 当我尝试添加这些方法作为参考时,注意到元数据存在问题。

WCF interface: 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: 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>

Now I can get data via GET request, but I want to be able use service with Web/Service reference like this.: 现在,我可以通过GET请求获取数据,但是我希望能够将服务与Web / Service引用一起使用,如下所示:

string s = MyServices.ActivationTest();

I assume to use methods like this, which returns or takes values other than int and string I should have [DataContracts]? 我假设使用这样的方法,该方法返回或采用我应该具有[DataContracts]的int和string以外的值? I understood too, I have to use [WebMethod] or [ScriptMethod], but I wasn't successful so far. 我也知道,我必须使用[WebMethod]或[ScriptMethod],但到目前为止我还没有成功。

Thanks in regards for any correction. 感谢您的任何纠正。

You can so it almost as smoothly as your example shows... Since your topic is quite broad I won't go in details just give pointers. 您可以使它几乎像您的示例所示的那样顺利进行...由于您的主题非常广泛,因此我将不做任何详细说明,而只给出指针。

For your own classes, to be able to use them as parameters and/or return types. 对于您自己的类,能够将它们用作参数和/或返回类型。 (in your case User ) you have to define what and how to be serialized. (在您的情况下为User ),您必须定义要序列化的内容和方式。 You can read about that here: 您可以在这里阅读有关内容:

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

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

Example: 例:

[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;

}

Now you are able to use your class. 现在,您可以使用您的课程了。

For calling the service: 致电服务:

You can add service reference: https://msdn.microsoft.com/en-us/library/bb386386.aspx (this would be a generated proxy to your service) 您可以添加服务参考: https : //msdn.microsoft.com/en-us/library/bb386386.aspx (这将是服务的生成代理)

Or you can use ChannelFactory: https://msdn.microsoft.com/en-us/library/ms734681(v=vs.110).aspx (with this you are in full control of the code, but might need to do more settings, ie endpoints.) 或者,您可以使用ChannelFactory: https : //msdn.microsoft.com/zh-cn/library/ms734681v = vs.110).aspx(使用此控件,您可以完全控制代码,但可能需要做更多操作设置,即端点。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM