WCF休息和肥皂

时间:2015-04-30 07:37:47

标签: wcf rest soap

我尝试使用soap和rest操作编写简单的订阅服务。这是我的代码: SubsService.svc:

namespace SubscriptionService
{
    public class SubsService : ISubsService
    {
        public string RegisterEvent(string serviceId, string name, string description)
        {
            return "ok";
        }

        public void UnregisterEvent(string serviceId, string name)
        {
            // TO DO
        }

        public void RiseEvent(string serviceId, string name)
        {
            // TO DO
        }

        public string EventsList(string token)
        {
            return token;
        }

        public void Subscribe(string token, string serviceId, string name)
        {
            // TO DO
        }

        public void UnSubscribe(string token, string serviceId, string name)
        {
            // TO DO
        }
    }
}

ISubsService.cs:

namespace SubscriptionService
{
    [ServiceContract]
    public interface ISubsService
    {
        // SOA
        [OperationContract]
        string RegisterEvent(string serviceId, string name, string description);

        [OperationContract]
        void UnregisterEvent(string serviceId, string name);

        [OperationContract]
        void RiseEvent(string serviceId, string name);

        // REST
        [OperationContract]
        [WebGet(UriTemplate = "events?token={token}")]
        string EventsList(string token);

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "subscribe?token={token}&serviceId={serviceId}&name={name}")]
        void Subscribe(string token, string serviceId, string name);

        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "unsubscribe?token={token}&serviceId={serviceId}&name={name}")]
        void UnSubscribe(string token, string serviceId, string name);
    }
}

和web.config:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="SubscriptionService.SubsService">
        <endpoint address="soap" binding="basicHttpBinding" contract="SubscriptionService.ISubsService"></endpoint>
        <endpoint address="rest" binding="webHttpBinding" contract="SubscriptionService.ISubsService" behaviorConfiguration="web"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <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>

当我尝试访问http://localhost:2354/SubsService.svc时,出现错误:

Operation 'RegisterEvent' of contract 'ISubsService' specifies multiple request body parameters to be serialized without any wrapper elements. At most one body parameter can be serialized without wrapper elements. Either remove the extra body parameters or set the BodyStyle property on the WebGetAttribute/WebInvokeAttribute to Wrapped.

但RegisterEvent必须是肥皂操作。我的代码出了什么问题? web.config中的东西?

0 个答案:

没有答案