Request format determination failed in soap service while calling rest service - c#

I am trying to call rest service which is creating with WebChannelFactory in soap service. I am using .net framework 4.0.
I have issue with request format.
Unit test and sample aspx page working as expected. But when calling rest service in soap method, request format setted as xml instead of json.
It is look like soap content-type request header used for new rest header.

We neeed new OperationContextScope for rest environment before request like below.
using (OperationContextScope scope = new OperationContextScope((IContextChannel)proxy))
{
proxy.myMethod();
}

Related

Consume SOAP webservice with GET instead of POST

I need to call an external SOAP webservice over HTTP.
I have the WSDL file and added it in Visual Studio via 'Add service reference'. Visual studio then added a number of files, in the reference file i can find this:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="Service.IService")]
public interface IService {
[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IService/Function", ReplyAction="http://tempuri.org/IService/FunctionResponse")]
namespace.Service.ExecuteFunctionResponse ExecuteFunction(namespace.Service.FunctionRequest request);
}
With additionaly the async version of this call and the objects for sending an receiving, etc.
To call the service I added the folowing code:
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpointAddress = new EndpointAddress("the address");
serviceChannel = new ServiceClient(binding, endpointAddress).ChannelFactory.CreateChannel();
Response response = serviceChannel.ExecuteFunction(new Request(...));
This results in me getting an exception, error 405 method not allowed.
So it appears I must use a HTTP GET request instead of the default POST request. But i cannot find where this can be altered in with this way of working.
So, where can i set the HTTP method for this call to a webservice?
SOAP services are using HTTP POST because they exchange XML messages (which tend to be complex) and cannot be transferred in a query string.
Are you sure that you must use HTTP GET? Maybe the error you are receiving "405 method not allowed" is caused by some bad configuration.
I would double check the SOAP endpoint URL is set correctly and check that there are no additional security requirements needed.
EDIT
In past, there was a practice to create ASP.NET Web Services which would accept GET as well. But they wouldn't expect XML messages. Instead, you would have to pass all parameters in a querystring. For example: https://foo.bar/service.asmx/Func?param1=X&param2=Y (where param1 and param2 are the expected parameters).
This way it is possible to call a WebService without a need for WSDL and using GET method. You can achieve it by using HttpClient for example.
The downside of this approach is that you will have to deal with plain data instead of objects.
I hope it might help.

how to get the xml representation of the SOAP request message?

I have the WSDL of a SOAP web service and I am consuming it via my MVC application.
From adding the WSDL as a web service to my Visual Studio solution it automatically creates a proxy class for me and it handles all the serialization/destabilization for me which is really awesome for a while. I have been using this proxy class to call/send my SOAP request to the web service (with pure c# code and no XML involves) and I got my response message back and everything is working great.
However, there is a need now for me to find what is the exact xml representation of the SOAP message that I am sending to the web service. How can I get/find/make this?
you can do it like this
var serxml = new System.Xml.Serialization.XmlSerializer(request.GetType());
var ms = new MemoryStream();
serxml.Serialize(ms, request);
string xml = Encoding.UTF8.GetString(ms.ToArray());
where xml is your raw SOAP

how to get the SOAP request of a Sharepoint web service from its method name

I would like to get the SOAP request XML of a Sharepoint web service by providing the method name.
For example, if I want the SOAP request message for the GetListCollection method, what do I need to do?

How do I expose a WCF web service call response header in C#

I am using Visual Studio 2008 and have added a web reference that points to a WCF web service.
Visual Studio has generated a client class automatically, so all I need to do to call the web service is to create an instance of the client and call the method on it.
FoodPreferenceServiceClient client = new FoodPreferenceServiceClient();
FoodPreferenceServiceResponse = client.GetFoodPreference();
The FoodPreferenceServiceClient is the web service client that is automatically generated by VS.
The GetFoodPreference is the method on the web service that I am calling.
My problem is that I want to expose the actual HTTP header received in the above call,
such as client.GetHttpResponse() or something.
Is there a way to do this?
Yes it should be possible. Try:
using (var scope = new OperationContextScope())
{
var client = new FoodPreferenceServiceClient();
response = client.GetFoodPreference();
var httpProperties = (HttpResponseMessageProperty)OperationContext.Current
.IncomingMessageProperties[HttpResponseMessageProperty.Name];
var headers = httpProperties.Headers;
// Now you should be able to work with WebHeaderCollection and find the header you need
}
you can't get the message headers through OeprationContext directly in client side, but you can develop a custom IClientMessageInspector, and in the interface you can get he SOAP XML message.

how to consume php web service in c# Desktop application

how to consume php web service in c# Desktop application. I am doing this by adding web reference and through code
WebReference.TestWSDL pdl = new testingApp.WebReference.TestWSDL();
string copy = pdl.verify("testing");
but it throws the error
Possible SOAP version mismatch: Envelope namespace http://schemas.xmlsoap.org/wsdl/ was unexpected. Expecting http://schemas.xmlsoap.org/soap/envelope/.
Make sure you are sending the the appropriate soap version request that the service is expecting ie sending a soap 1.2 request to a service expecting a 1.1 request would give a similar error. Maybe run fiddler and post the messages that are sent and recieved for people to have a look at?

Categories

Resources