Getting null values in post wcf service - wcf

I am writing a WCF service. Here is Service side implementation
[OperationContract(Name="GetMediaFile")]
[Description(ServiceDescConstants.GetMediaFile)]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = UriTemplateConstants.GetMediaFile)]
Stream GetMediaFile(string type, string mediaId);
where
UriTemplateConstants.GetMediaFile = "GetMediaFile?type={type}&mediaId={mediaId}";
Here is interface method implementation
public Stream GetMediaFile(string type, string mediaId)
{
CustomerBL customerBl = new CustomerBL();
return customerBl.getMediaFile(Convert.ToInt32(type), Convert.ToInt32(mediaId));
}
On client side I am using RestClient plugin for testing service.
Here is data I am sending
URL : customersite/GetMediaFile
Header : Content-Type = x-www-form-urlencoded
Body : type=0&mediaId=1
Any help !!
Now the PROBLEM is that I am getting null values

Modify the interface method:
[OperationContract(Name = "GetMediaFile")]
[Description(ServiceDescConstants.GetMediaFile)]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "/GetMediaFile")]
Stream GetMediaFile(Stream input);
And also modify it's implementation:
public Stream GetMediaFile(Stream input)
{
StreamReader sr = new StreamReader(input);
string s = sr.ReadToEnd();
sr.Dispose();
NameValueCollection qs = HttpUtility.ParseQueryString(s);
string type = qs["type"];
string mediaId = qs["mediaId"];
CustomerBL customerBl = new CustomerBL();
return customerBl.getMediaFile(Convert.ToInt32(type), Convert.ToInt32(mediaId));
}
In the web.config, use the following configuration (make sure that you use your own namespaces / classes / interfaces names):
<system.serviceModel>
<services>
<service name="WcfServices.MyService">
<endpoint address=""
name="webEndPoint"
behaviorConfiguration="webBehavior"
binding="webHttpBinding"
contract="WcfServices.IMyService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
This is a sample request:
POST /MyService.svc/GetMediaFile HTTP/1.1
Host: localhost:64531
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
type=0&mediaId=1
The solution is an adaptation of Edgardo Rossetto's blog article, Raw HTTP POST with WCF.

Related

AJAX POST to WCF web config need another set of eyes/mind

For some reason I'm not able to get a very simple AJAX post to my web service (svc) to work.
I'm sure it's a problem with my web.config, but I tried everything I can find online.
In "MyService.svc" if is use
[OperationContract]
[WebInvoke(Method ="POST", RequestFormat = WebMessageFormat.Json, ResponseFormat =WebMessageFormat.Json, BodyStyle =WebMessageBodyStyle.Wrapped)]
public string DoWork()
{
string result = "did it";
//result = test;
return (new JavaScriptSerializer().Serialize(result)); ;
}
where DoWork only returns a value, Ajax succeeds. However, if I pass a parameter to DoWork(string test)
Ajax returns an error. Bad Request.
My ajax is:
function DoTest() {
testData = { "test": "Hello" };
var jsonData = JSON.stringify(testData);
var POSTURL = "MyService.svc/DoWork";
//alert(jsonData);
//alert(POSTURL);
$.ajax({
type: "POST",
url: POSTURL,
datatype: "json",
data: jsonData,
ContentType: "application/json; charset=utf-8",
success: function (result) {
alert("success: " + result.d);
},
error: function (xhr, status, error) {
alert("Opps: " + xhr + " " + status + " " + error);
}
})
}
});
This is in my web.config:
<behaviors>
<endpointBehaviors>
<behavior name="MyServiceAspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint address="" behaviorConfiguration="MyServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="MyService" />
</service>
</services>
</system.serviceModel>
I need another set of eyes on this otherwise simple program. Thanks
"Bad Request" is usually an error in the request format. You can use the help document in WCF to view the request format. If you want to enable the help document, you need to change the WebMessageBodyStyle to bare:
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle =WebMessageBodyStyle.Bare)]
Then enable the help document in the configuration file:
<endpointBehaviors>
<behavior name="ESEndPointBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
View the requested format in the browser, you need to send the data in its format.
I removed an extra <endpoint address="" I had in my revision. The service call is being made from AJAX now. Phew. This WCF works great when it's working, and very difficult to diagnose when it does not work. Thanks for listening and good luck to everyone.

Inserting Data in WCF REST Service showing Error : The remote server returned an error: (404) Not Found

I have created the Two Methods one is to Get and another one is to Create,
For Getting the Data is working fine, but when i trying to post the Data is showing the Error like Method not Found(404)
The Code is as following.
Contract Methods :
public interface IContactPositionService {
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "GET",RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetContactPositionList")]
List<ContactPosition> GetContactPositionList();
[OperationContract]
[WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.WrappedRequest,RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "AddContactPosition")]
int AddContactPosition(ContactPosition position);}
And Corresponding Serivice class is like :
public class ContactPositionService : IContactPositionService
{
public List<ContactPosition> GetContactPositionList()
{
DataSet ds = clsObj.GetContactPositionsList();
return ConverterHelper.Convert<ContactPosition>(ds.Tables[0]);
}
public int AddContactPosition(ContactPosition position)
{
// throw new System.NotImplementedException();
}
And in Web.config file i have configured the End Points As :
<services>
<service name="VirtusMobileService.ContactPositionService" behaviorConfiguration="VMS.Position.ServiceBehaviour">
<endpoint address="" behaviorConfiguration="VMS.Position.EndPointBehaviour"
binding="webHttpBinding" bindingConfiguration="" name="VMS.Position.EndPoint"
contract="VirtusMobileService.IContactPositionService" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="VMS.Position.EndPointBehaviour">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="VMS.ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="VMS.Position.ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
From the Client Asp.net page we call the Services for adding the Contact person object as like below :
private void AddData()
{
string sURL = "http://localhost:51293/ContactPositionService.svc/AddContactPosition";
ContactPosition order = new ContactPosition
{
PositionCode="10550",
PositionFinbaseId=11,
PositionTitle="Manager"
};
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ContactPosition));
MemoryStream mem = new MemoryStream();
ser.WriteObject(mem, order);
string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
webClient.UploadData(sURL, "PUT", mem.ToArray()); // Here it is showing Error
Console.WriteLine("Order placed successfully...");
}
While it comes to , " uploadData " it is showing error like "
remote server returned an error: (404) Not Found."
If i am trying to Get the Data using the "GetContactPositionList"
it showing the data Correctly , but when i am trying to work with
"PUT" method it is showing that error.
I tested the url in Fiddler , it is showing the same error in Response
Header "HTTP/1.1 405 Method Not Allowed"
<div id="content">
<p class="heading1">Service</p>
<p xmlns="">Method not allowed. Please see the <a rel="help-page" href="http://localhost:51293/ContactPositionService.svc/help">service help page</a> for constructing valid requests to the service.</p>
</div>
Please Suggest the answer.
Thanks.
In fiddler you have to specify application/json as Content Type and it will hit the service Try this link
Instead of "webClient.UploadData", try this..
webClient.UploadString(sURL, "PUT", data);
I think you could probably make this much easier on yourself by using System.ServiceModel.ChannelFactory. It will simplify greatly your interaction with the service, as you will have strongly-typed variables, the method names themselves, and not have to mess with the serialization yourself.
You can replace your AddData method as:
private void AddData()
{
// Create the service channel; 'factory' and/or 'svc' can be class members so we can re-use them.
var factory = new ChannelFactory<IContactPositionService>(new WebHttpBinding(), new EndpointAddress("http://localhost:51293/ContactPositionService.svc"));
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
var svc = factory.CreateChannel();
ContactPosition order = new ContactPosition
{
PositionCode = "10550",
PositionFinbaseId = 11,
PositionTitle = "Manager"
};
try
{
var result = svc.AddContactPosition(order);
Console.WriteLine("Order# " + result.ToString() + " placed successfully...");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Note: This will require your calling project to have a reference to System.ServiceModel.dll and System.ServiceModel.Web.dll. Additionally, the calling project will need to know about IContactPositionService and ContactPosition. I your calling project is not the same as your WCF project, and does not already reference your WCF project, then you can move your service interfaces and objects into a third class library, which you could then share between the WCF host and any consumers.

How to configure WCF services

I'm new to using WCF services and i hope you can help me out on some problems invoking an external REST webservice. I have create two applications to try and test my service.
Console application to test my class lib
An umbraco application which hosts my services
Both localhost.
Here is the code of my service:
namespace HorecaWebservices.Webservices
{
[ServiceContract]
public interface IWS_Test
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
Agenda GetAgenda(String mobileAppKey);
}
}
namespace HorecaWebservices.Webservices
{
public class WS_Test : IWS_Test
{
public Agenda GetAgenda(String mobileAppKey)
{
return BL_Agenda.GetAgenda(mobileAppKey);
}
}
}
Web config of my service:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true"
automaticFormatSelectionEnabled="true"
defaultOutgoingResponseFormat="Json">
</standardEndpoint>
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="HorecaWebservices.Webservices.WS_Test"
behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="wsHttpBinding"
contract="HorecaWebservices.Webservices.IWS_Test"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding" address="mex"/>
</service>
</services>
</system.serviceModel>
Here is the code from my console application which invokes the service method:
public static class BL_Agenda
{
public static Agenda GetAgenda()
{
Agenda agenda = new Agenda();
WebRequest request = WebRequest.Create("http://localhost:63462/Webservices/WS_Test.svc/GetAgenda");
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
string postData = "{\"mobileAppKey\":\"HEMDZ\"}"; //encode your data
using (Stream s = request.GetRequestStream())
{
using (StreamWriter sw = new StreamWriter(s))
sw.Write(postData);
s.Close();
}
try
{
using (Stream s = request.GetResponse().GetResponseStream())
{
using (StreamReader sr = new StreamReader(s))
{
var jsonData = sr.ReadToEnd();
agenda = JsonConvert.DeserializeObject<Agenda>(jsonData);
}
s.Close();
}
}
catch (WebException e)
{
agenda = null;
WebExceptionStatus status = e.Status;
Console.WriteLine(status.ToString());
}
return agenda;
}
Now whenever i run this code the request.GetResponse() throws a System.Net.WebException "(415) Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'"
After a couple of hours trying to find out what the problem is i still cant figure it out.. Can someone help me out on this?
At a first glance your endpoint uses the wrong binding (wsHttpBinding):
<endpoint address="" binding="wsHttpBinding" contract="HorecaWebservices.Webservices.IWS_Test"/>
Whereas it should be webHttpBinding:
<endpoint address="" binding="webHttpBinding" contract="HorecaWebservices.Webservices.IWS_Test"/>
For a complete webHttp-sample binding take a look at this article: http://weblogs.asp.net/kiyoshi/archive/2008/10/08/wcf-using-webhttpbinding-for-rest-services.aspx
When changing the binding other issues are likely to occur. One thing is, that you'll probably don't need the <standardEndpoints>-node any more. So I'd suggest staying close to the linked sample.
Additionally it's always a good idea to activate tracing. Having tracing activated you'll get more detailed information about your 500 error (and probably others too).
This example is not exactly the same, but maybe the Factory attribute suggested will give you some clues.
HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'
I've got it! I finally sorted out the bad request error. I had to replace the BodyStyle = WebMessageBodyStyle.Bare setting to BodyStyle = WebMessageBodyStyle.WrappedRequest.
Thnx for all the help guys!

Is it possible to set the request/response format in web.config?

I have quite a few endpoints on my WCF REST Service. They all have the same body style, request format and response format.
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = xxx)]
Is there anyway to set those attributes in the web.config?
You can set the default value for the body style and for the outgoing response format (not for the request format) in the <webHttp> endpoint behavior (see below). Notice that if you're in a service (which seems to be your case), that doesn't matter, since WCF REST endpoints can receive requests in both XML and JSON - the RequestFormat property is only used when it's being used within a client, to decide in which format to send the request.
<endpointBehaviors>
<behavior name="WebWithDefaults">
<webHttp defaultOutgoingResponseFormat="Json"
defaultBodyStyle="Bare" />
</behavior>
</endpointBehaviors>

WCF webHttpBinding post to apache service

I am trying to communicate with an Apache service from a WCF client
I have set up the client like this:
<client>
<endpoint name="ApacheService"
address="SomeUrl"
behaviorConfiguration="ApacheBehavior"
binding="webHttpBinding"
contract="ISomeContrect" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="ApacheBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
and my contract looks like
[OperationContract]
[WebInvoke(Method = WebRequestMethods.Http.Post,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "?user={username}&action=someaction")]
void dosomeaction(string username, List<SomeJSONSerializableObject> data);
Don't know what framework is used on the receiving side but the user and action variables are presented as get variables and the json payload is not seen at all.
debug started
post:
Array
(
)
get:
Array
(
[user] => someusername
[action] => someaction
)
json:
I did some local tests with a WCF service and it works fine.
Any ideas what ca be wrong?
Never mind. It was some error on the Apache side