Wcf System.ServiceModel.ProtocolException:"远程服务器返回意外响应:(400) 错误请求。



我正在尝试测试我的wcf服务。app. i Service将其引用到Console App.i获取此错误;

System.ServiceModel.Protocolexception:'远程服务器返回了意外响应:(400)不良请求。'

内部异常WebException:远程服务器返回错误:(400)不良请求

编辑!!

错误:System.ServiceModel.protocolexception:远程服务器返回了意外响应:(405)不允许的方法。---> system.net.webexception:远程服务器返回错误:(405)不允许的方法。 在system.net.httpwebrequest.getResponse() 在System.ServiceModel.Channels.httpChannelfactory 1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) --- End of inner exception stack trace --- Server stack trace: at System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory 1工厂,WebException响应Exception,ChannelBinding ChannelBinding) 在System.ServiceModel.Channels.httpChannelfactory`1.httpRequestChannel.httpChannelRequest.Waitforreply(timespan超时) at System.ServiceModel.Channels.RequestChannel.Request(消息消息,时机超时) 在System.ServiceModel.disPatcher.RequestChannelBinder.Request(消息消息,timespan超时) at System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔值,proxyoperationRuntime操作,对象[] ins,object [] outs,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 users aykut source source repos work work wstester wstester connected services workws reference.cs:第291行291 在c: users yykut source repos work work wstester program.cs.cs中的wstester.program.main(string [] args)中

thanx寻求建议。这是我的app.config in Console App

<?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>

,这是我的Web.config WebService:

<?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);
    }

编辑!! 这是我的服务1.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;
    }

我找到了解决方案。当我在SQL Server地址上实施到表格时,无法为null,它们已经映射了,因此不需要保存(customer.addresses)。

    public Customer CreateCustomer(Customer customer)
    {
        ISession session = this.GetSession();
        using (var tx = session.BeginTransaction())
        {
            try
            {
                this.GetSession().Save(customer);
                tx.Commit();
            }
            catch(Exception e)
            {
                tx.Rollback();
            }
        }
        return customer;
    }

相关内容

  • 没有找到相关文章

最新更新