【发布时间】:2017-11-22 07:17:17
【问题描述】:
我正在尝试测试我的 WCF 服务,它包括方法 create、update、getlist 到 Sql Server 与。它在浏览器上工作 GET 方法,但我想在控制台应用程序上测试 POST 方法。所以我服务参考它控制台应用程序。我收到此错误;
System.ServiceModel.ProtocolException: '远程服务器返回了意外响应:(400) 错误请求。'
内部异常 WebException: 远程服务器返回错误:(400) Bad Request
编辑!!
错误:
System.ServiceModel.ProtocolException:远程服务器返回意外响应:(405)方法不允许。 ---> System.Net.WebException:远程服务器返回错误:(405)方法不允许。
在 System.Net.HttpWebRequest.GetResponse()
在 System.ServiceModel.Channels.HttpChannelFactory1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
--- End of inner exception stack trace ---
Server stack trace:
at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory1 工厂,WebException responseException,ChannelBinding channelBinding)
在 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan 超时)
在 System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan 超时)
在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan 超时)
在 System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔单向,ProxyOperationRuntime 操作,Object[] 输入,Object[] 输出,TimeSpan 超时)
在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime 操作)
在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)
在 [0] 处重新抛出异常:
在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)
在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 类型)
在 WSTester.WorkWS.IService1.GetCustomerList()
在 C:\Users\aykut\source\repos\Work\WSTester\Connected Services\WorkWS\Reference.cs:line 291 中的 WSTester.WorkWS.Service1Client.GetCustomerList()
在 C:\Users\aykut\source\repos\Work\WSTester\Program.cs:line 47 中的 WSTester.Program.Main(String[] args) 处
感谢您的建议。 这是我在控制台 APP 中的 App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP"
maxReceivedMessageSize="1048576"/>
</webHttpBinding>
</bindings>
<client>
<endpoint address="http://192.168.0.XX:7767/Service1.svc"
binding="webHttpBinding"
contract="WorkWS.IService1" behaviorConfiguration="webhttp" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="webhttp">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
这是我在 WebService 中的 Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler,NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;;Initial Catalog=FirstProject; User Id = aykut ; password= 1234</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="HBM_ASSEMBLY" value="BusinessEntities" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<identity impersonate="false" />
<authentication mode="Forms" />
</system.web>
<system.serviceModel>
<services>
<service name="WebService.Service1">
<endpoint address="" behaviorConfiguration="webHttpBehavior" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="WebService.IService1" />
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- Meta veri bilgilerini açığa çıkarmaktan kaçınmak için, dağıtımdan önce aşağıdaki bilgileri false yapın -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- Hatalarda hata ayıklamak amacıyla özel durum ayrıntıları almak için aşağıdaki değeri true yapın. Özel durum bilgilerini açığa çıkarmaktan kaçınmak için dağıtımdan önce false yapın -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<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>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Iesi.Collections" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
这是我的主要方法;
static void Main(string[] args)
{
Service1Client client = new Service1Client();
Customer c = new Customer();
c.Name = "dsfsfsdf";
c.SurName="asdass";
c.Age = 12;
client.CreateCustomer(c);
}
编辑!! 这是我的 Service1.svc:
public class Service1 : IService1
{
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "GetCustomerList")]
public List<Customer> GetCustomerList()
{
CustomerFinder cm = new CustomerFinder();
List<Customer> customerlist = cm.GetAllCustomers();
return customerlist;
}
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "GetCustomerByID/{id}")]
public Customer GetCustomerByID(string id)
{
CustomerFinder cm = new CustomerFinder();
Customer customer = cm.FindByID(Int32.Parse(id));
return customer;
}
[WebInvoke(Method = "POST",
UriTemplate = "CreateCustomer",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public Customer CreateCustomer(Customer customer)
{
if (customer == null)
{
throw new ArgumentNullException("FAIL");
}
CustomerManager cm = new CustomerManager();
cm.CreateCustomer(customer);
return customer;
}
}
这是 IService:
public interface IService1
{
[OperationContract]
List<Customer> GetCustomerList();
[OperationContract]
Customer GetCustomerByID(string id);
[OperationContract]
Customer CreateCustomer(Customer customer);
[OperationContract]
void DeleteCustomerByID(string id);
}
}
这是我的 CreateCustomer 方法:
public class CustomerManager : BusinessComponentBase
{
public Customer CreateCustomer(Customer customer)
{
ISession session = this.GetSession();
using (var tx = session.BeginTransaction())
{
try
{
this.GetSession().Save(customer);
this.GetSession().Save(customer.Addresses);
tx.Commit();
}
catch
{
tx.Rollback();
}
}
return customer;
}
【问题讨论】:
-
问题出在服务器端。请发布一些代码。
标签: c# web-services wcf iis nhibernate