Reputation: 58
I'm trying to test my WCF service ıt is include methods create,update,getlist to Sql Server vs. .It works GET method on browser but i want to test POST method on Console app.So i service reference its to console app.I get this error ;
System.ServiceModel.ProtocolException: 'The remote server returned an unexpected response: (400) Bad Request.'
Inner Exception WebException: The remote server returned an error: (400) Bad Request
EDIT!!
ERROR:
System.ServiceModel.ProtocolException: The remote server returned an unexpected response: (405) Method Not Allowed. ---> System.Net.WebException: The remote server returned an error: (405) Method Not Allowed.
at System.Net.HttpWebRequest.GetResponse()
at 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, HttpChannelFactory
1 factory, WebException responseException, ChannelBinding channelBinding)
at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at WSTester.WorkWS.IService1.GetCustomerList()
at WSTester.WorkWS.Service1Client.GetCustomerList() in C:\Users\aykut\source\repos\Work\WSTester\Connected Services\WorkWS\Reference.cs:line 291
at WSTester.Program.Main(String[] args) in C:\Users\aykut\source\repos\Work\WSTester\Program.cs:line 47
Thanx for advice. This is my 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>
And here is my Web.config in 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>
And here is the my main method;
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);
}
EDIT!! This is my 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;
}
}
This is IService:
public interface IService1
{
[OperationContract]
List<Customer> GetCustomerList();
[OperationContract]
Customer GetCustomerByID(string id);
[OperationContract]
Customer CreateCustomer(Customer customer);
[OperationContract]
void DeleteCustomerByID(string id);
}
}
And this is my CreateCustomer method:
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;
}
Upvotes: 0
Views: 3040
Reputation: 58
I find the solution..Problem is on CreateCustomer method. When i implement to my tables on sql server addresses can not be null and they already mapped so do not need for Save(customer.Addresses) this way;
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;
}
Upvotes: 0