我正在编写程序的一部分,用于客户端和服务器之间的通信。
服务仅将查询转发到数据库。但是当我尝试发送查询时,我得到一个异常“ProtocolException /(405)Method not allowed”。
我尝试了ProtocolException Unhandled/(405) Method not allowed with WCF; Bindings and Endpoints look right though的答案,但没有任何帮助。
以下是我的一些文件:
沟通客户这是图书馆,因为我们想在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; }
}
}
}
的ServiceContract
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的子代。
答案 0 :(得分:0)
您的服务合同名称是ITask,而您的服务类实现了一些不同的接口,即IServerTask。请更正服务类定义。