繁体   English   中英

使用ChannelFactory的WCF客户端,方法返回ProtocolException / 405 Method not Allowed

[英]WCF Client with ChannelFactory, method returns ProtocolException / 405 Method not Allowed

我正在编写程序的一部分,用于客户端和服务器之间的通信。

服务仅将查询转发到数据库。 但是当我尝试发送查询时,我得到一个异常“ProtocolException /(405)Method not allowed”。

我试过来自ProtocolException Unhandled /(405)WCF不允许的方法; 绑定和端点看起来不错 ,但没有任何帮助。

以下是我的一些文件:

用于通信的客户端它是库,因为我们想要从Unity使用它,我也希望在测试中使用此代码。

namespace Client
{
    public class ClientCommunicationWcf : IDisposable
    {
        private readonly ChannelFactory<ITask> _taskFactory;


        public ClientCommunicationWcf()
        {
            _taskFactory = new ChannelFactory<ITask>("localhost");
        }

        public T GetResponse<T>(string commandName, object data)
        {
            var channel = _taskFactory.CreateChannel();
            channel.Execute(commandName, data);
            return (T)channel.ResponseObject;
        }

        public void Dispose()
        {
            _taskFactory.Close();
            ((IDisposable) _taskFactory).Dispose();
        }
    }
}

DataContract

namespace CommunicationCommonLib.Requests
{
    [DataContract]
    [KnownType(typeof(LoginUserRequest))]
    public class LoginUserRequest
    {
        [DataMember]
        private readonly string _username;
        [DataMember]
        private readonly string _password;


        public LoginUserRequest(string username, string password)
        {
            _username = username;
            _password = password;
        }


        public string Username
        {
            get { return _username; }
        }

        public string Password
        {
            get { return _password; }
        }
    }
}

服务合同

namespace CommunicationCommonLib
{
    [ServiceContract]
    public interface ITask
    {
        object ResponseObject
        {
            [OperationContract]
            get;
        }

        /// <param name="data"></param>
        [OperationContract]
        void Execute(string commandName, object data);
    }
}

服务 :IServerTask是ITask的孩子

namespace WcfService2
{
    public class ServerTaskService : IServerTask, ITask
    {
        private object _responseObject;


        public object ResponseObject
        {
            get { return _responseObject; }
        }


        public void Execute(string commandName, object data)
        {
            DataCommands.RunCommand(commandName, data, this);
        }

        public void SetResponse(object response)
        {
            _responseObject = response;
        }
    }
}

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>

    <behaviors>
      <serviceBehaviors>
        <behavior name="WcfService2.ServerTaskServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <services>
      <service behaviorConfiguration="WcfService2.ServerTaskServiceBehavior" name="WcfService2.ServerTaskService">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding" contract="CommunicationCommonLib.ITask" />
      </service>
    </services>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />


    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding" closeTimeout="00:01:00" openTimeout="00:01:00"
          receiveTimeout="01:00:00" sendTimeout="04:00:00" allowCookies="false"
          bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="209715200" maxBufferSize="52428800" maxReceivedMessageSize="52428800"
          textEncoding="utf-8" transferMode="Streamed" useDefaultWebProxy="false"
          messageEncoding="Mtom">
          <readerQuotas maxStringContentLength="10485760" maxArrayLength="52428800" />
          <security mode="None">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

  </system.serviceModel>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

App.config中

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.serviceModel>
    <client>
      <endpoint address="http://localhost:55555/Task" binding="basicHttpBinding" contract="CommunicationCommonLib.ITask" name="localhost"/>
    </client>
  </system.serviceModel>
</configuration>

地址“ http:// localhost:5555 / Task ”也在WcfService2 - Properties - Web中设置,它使用IIS Express。 我编写了用于测试客户端服务器通信的WPF应用程序,其中存储了App.config。 WPF仅用于发送请求和检查结果。

Web.config可能是错误的,因为它是我的第一个WCF,我尝试了不同的例子。

当我运行程序时,浏览器打开“ http:// localhost:5555 / Task ”,所以我认为该服务正在运行。

感谢帮助。

编辑: ServerTaskService是IServerTask和ITask的子代。

您的服务合同名称是ITask,而您的服务类实现了一些不同的接口,即IServerTask。 请更正服务类定义。

暂无
暂无

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

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