I designed a WCF REST service and try to consume it via JQuery in html page. When I added the html page in the solution where WCF service was created, I was getting the result as per the need. But when I created the HTML Page in a different solution I started getting errors such as no element found and the returned result is null.
I put a debugger in the service and I noticed that even if the the Consumer html page is kept in a different solution it still hits the global.asax file when service is called, But its not hitting any of the Method.
I even put "crossDomain:true," and "jQuery.support.cors = true;" in script for enabling Cross Domain problem as well as I have made changes in Global.asax file too but of no use.
Please provide me the solution for the same asap.
I am placing the actual code with configuration files and DataContracts along with the HTML code here :
ICouponService.cs :
[ServiceContract]
public interface ICouponService
{
[OperationContract]
[WebInvoke(UriTemplate = "/GenerateCoupon",
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
CouponData GenerateCoupon(CouponData objCustomerData);
[OperationContract]
[WebInvoke(UriTemplate = "/UpdateGeneratedCoupon",
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
ResponseMessage UpdateGeneratedCoupon(CouponData objCustomerData);
}
CouponService.cs :
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class CouponService : ICouponService
{
public CouponData GenerateCoupon(CouponData objCustomerData)
{
return objCouponData;
}
public ResponseMessage UpdateGeneratedCoupon(CouponData objCustomerData)
{
return objResponseMessage;
}
}
CouponData.cs :
[DataContract]
public class CouponData
{
[DataMember(EmitDefaultValue = false)]
public string Email { get; set; }
[DataMember(EmitDefaultValue = false)]
public string Points { get; set; }
[DataMember(EmitDefaultValue = false)]
public string ActiveFlag { get; set; }
[DataMember(EmitDefaultValue = false)]
public string CouponCode { get; set; }
[DataMember(EmitDefaultValue = false)]
public string RequestID { get; set; }
[DataMember(EmitDefaultValue = false)]
public string LoyaltyID { get; set; }
[DataMember(EmitDefaultValue = false)]
public string Result { get; set; }
[DataMember(EmitDefaultValue = false)]
public string ReturnFlag { get; set; }
[DataMember(EmitDefaultValue = false)]
public string UserName { get; set; }
[DataMember(EmitDefaultValue = false)]
public string Password { get; set; }
}
ResponseMessage.cs :
[DataContract]
public class ResponseMessage
{
[DataMember(EmitDefaultValue = false)]
public string Response { get; set; }
}
Global.asax :
First I thought there might be some Header problem so I added the same in Global file as shown below :
protected void Session_Start(object sender, EventArgs e)
{
string sessionId = Session.SessionID;
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
EnableCrossDomainAjaxCall();
}
private void EnableCrossDomainAjaxCall()
{
if (!string.IsNullOrEmpty(Request.ServerVariables["HTTP_ORIGIN"]))
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", Request.ServerVariables["HTTP_ORIGIN"]);
if (!string.IsNullOrEmpty(HttpContext.Current.Request.HttpMethod))
{
if (HttpContext.Current.Request.HttpMethod == "OPTIONS" || HttpContext.Current.Request.HttpMethod == "POST" || HttpContext.Current.Request.HttpMethod == "GET")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
//HttpContext.Current.Response.End();
}
}
}
}
Web.config :
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="SAM_STORE_LoyaltyProgram_WCFLibrary.CouponService">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
contract="SAM_STORE_LoyaltyProgram_WCFLibrary.ICouponService" />
</service>
<service name="SAM_STORE_LoyaltyProgram_WCFServices.qqq">
<endpoint address="" behaviorConfiguration="SAM_STORE_LoyaltyProgram_WCFServices.qqqAspNetAjaxBehavior"
binding="webHttpBinding" contract="SAM_STORE_LoyaltyProgram_WCFServices.qqq" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
<behavior name="SAM_STORE_LoyaltyProgram_WCFServices.qqqAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<dataContractSerializer maxItemsInObjectGraph="5000" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<bindings>
<webHttpBinding>
<binding maxBufferSize="5000" maxReceivedMessageSize="5000"
transferMode="Streamed">
<readerQuotas maxDepth="5000" maxStringContentLength="5000"
maxArrayLength="5000" maxBytesPerRead="5000" maxNameTableCharCount="5000" />
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Index.html (script used to call service):
<script src="js/jquery-1.7.min.js" type="text/javascript"></script>
<script type="text/javascript">
var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;
var method;
var eMail;
var points;
var requestId;
var couponCode;
var returnFlag;
var userName;
var password;
//Generic function to call Service
function CallService() {
$.ajax({
type: Type,
url: Url,
data: Data,
contentType: ContentType,
dataType: DataType,
//crossDomain:true, --This is not working
processdata: ProcessData,
success: function (msg) {
ServiceSucceeded(msg);
},
error: function (msg) {
alert("Service Failed");
ServiceFailed(msg);
}
});
}
function SetGenerateCouponValues() {
eMail = $("#txtEmail").val();
points = $("#txtPoints").val();
requestId = $("#txtRequestId").val();
userName = 'username';
password = 'JEBtJHVu';
}
function DestroyGenerateCouponValues() {
eMail = null;
points = null;
requestId = null;
userName = null;
password = null;
}
function SetUpdateCouponValues() {
couponCode = $("#txtCode").val();
returnFlag = $("#txtFlag").val();
userName = 'username';
password = 'JEBtJHVu';
}
function DestroyUpdateCouponValues() {
couponCode = null;
returnFlag = null;
userName = null;
password = null;
}
function ServiceFailed(result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null; Url = null; Data = null; ContentType = null; DataType = null; ProcessData = null;
}
function GenerateCoupon() {
SetGenerateCouponValues();
Type = "POST";
Url = "ServiceURL";
Data = '{ "Email": "' + eMail + '", "Points": "' + points + '", "UserName": "' + userName + '", "Password": "' + password + '", "RequestID": "' + requestId + '"}';
ContentType = "application/json; charset=utf-8";
DataType = "json";
ProcessData = true;
method = "GenerateCoupon";
CallService();
DestroyGenerateCouponValues();
}
function UpdateGeneratedCoupon() {
SetUpdateCouponValues();
Type = "POST";
Url = "ServiceURL";
Data = '{ "CouponCode": "' + couponCode + '", "ReturnFlag": "' + returnFlag + '", "UserName": "' + userName + '", "Password": "' + password + '"}';
ContentType = "application/json; charset=utf-8";
DataType = "json";
ProcessData = true;
method = "UpdateGeneratedCoupon";
CallService();
DestroyUpdateCouponValues();
}
function ServiceSucceeded(result) {
if (DataType == "json") {
if (method == "GenerateCoupon") {
var string = "Coupon Code : " + result.CouponCode + "<br/>" + "Points : " + result.Points + "<br/>" + "Result : " + result.Result;
$("#resultCreate").html(string);
$("#lblCouponCode").text(result.CouponCode);
$("#lblPoints").text(result.Points);
$("#lblResult").text(result.Result);
}
else if (method == "UpdateGeneratedCoupon") {
var string = "Response : " + result.Response;
$("#resultDelete").html(string);
$("#lblResponse").text(result.Response);
}
}
}
/*function ServiceFailed(xhr) {
alert("FAIL" + xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}*/
$(document).ready(
function () {
//jQuery.support.cors = true; --This is not working
$("#btnsubmit").click(function () {
GenerateCoupon();
});
$("#btnUpdateGeneratedCoupon").click(function () {
UpdateGeneratedCoupon();
});
});
</script>
Please provide me the solution for the same asap.
Is it calling ServiceSucceeded method??
If ServiceSucceeded called try to put $.support.cors = true; in CallService() starting and then check it
Try enabling WCF tracing and ETW tracing to see what is happening at the server side while invoking the service operation.
Related
I have a struggling time with WCF REST to parse request body.
My problem:
POST method https://{Service}/X/Y? Z= 0000000
Body Request: {"id":"id"}
My code:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/X/{y}?Z={z}")
]
string GetRequest(string y, string z, Request request);
}
public class Service : IService
{
public string GetRequest(string y, string z, Request request)
{
//Do sth
}
[DataContract]
public class Request
{
[DataMember]
[JsonProperty("id")]
public String Id { get; set; }
}
The problem that I have is that y and z has data and they are correct BUT id in request is null. I expected to be "id".
I searched a lot in the internet and I found Stream solution which is not easily to follow in this case.
I am wondering if anyone has a clever idea to do this.
Take a look on this version
The Service
namespace ServiceTest
{
internal class Program
{
private static WebServiceHost _service;
private static void Main()
{
_service = new WebServiceHost(typeof (Service));
Console.WriteLine("The service has started");
_service.Open();
Console.ReadKey();
}
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public sealed class Service : IService
{
public string GetRequest(string y, string z, Request request)
{
Console.WriteLine("Y: {0}, Z: {1}, request: {2}", y, z, request);
return "Ok";
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, UriTemplate = "/X/{y}?Z={z}")]
string GetRequest(string y, string z, Request request);
}
[DataContract]
public sealed class Request
{
[DataMember]
public string Id { get; set; }
public override string ToString()
{
return string.Format("Id: {0}", Id);
}
}
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="ServiceTest.Service">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9090/webhost" />
</baseAddresses>
</host>
<endpoint binding="webHttpBinding" contract="ServiceTest.IService" />
</service>
</services>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
The Client
internal class Program
{
private static void Main(string[] args)
{
var client = new HttpClient();
var request = new Request {Id = "Nelibur"};
var result = client.PostAsync("http://localhost:9090/webhost/X/Y?Z=2000", CreateContent(request)).Result;
Console.WriteLine(result.Content.ReadAsStringAsync().Result);
Console.ReadKey();
}
private static StringContent CreateContent<T>(T value)
{
using (var stream = new MemoryStream())
{
var serializer = new DataContractJsonSerializer(typeof (T));
serializer.WriteObject(stream, value);
string content = Encoding.UTF8.GetString(stream.ToArray());
return new StringContent(content, Encoding.UTF8, "application/json");
}
}
}
Raw request:
POST http://localhost:9090/webhost/X/Y?Z=2000 HTTP/1.1
Content-Type: application/json; charset=utf-8
Host: localhost:9090
Content-Length: 16
Expect: 100-continue
Connection: Keep-Alive
{"Id":"Nelibur"}
You can try to use the request in for example Fiddler
I have two post methods which return json result. one is update_existing_user able to access by client but another one submit_reg_data unable to access. I didn't understand why did the update_existing_user is accessed by client and submit_reg_data doesn't access by client. but I was tried access these two methods either from postman or from firefox httpRequester I got perfect result.
interface:
namespace MFAB
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMFABService" in both code and config file together.
[ServiceContract]
public interface IMFABService
{
[WebInvoke(Method = "POST", UriTemplate = "/submit_reg_data",
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract]
Stream submit_reg_data(RegisterProp objResterProperties);
[WebInvoke(Method = "POST", UriTemplate = "/update_existing_user",
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract]
Stream update_existing_user(RegisterProp RegisterProperties);
}
}
class.svc---
namespace MFAB
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MFABService" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select MFABService.svc or MFABService.svc.cs at the Solution Explorer and start debugging.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MFABService : IMFABService
{
Validation objValidation = new Validation();
static long intLeadRegisterID = 0;
public Stream update_existing_user(RegisterProp objRegisterProperties)
{
// RegisterProp objRegisterProperties = new RegisterProp();
// objRegisterProperties = JsonHelper.JsonDeserialize<RegisterProp>(RegisterProperties);
RegisterBO objRegisterBO = new RegisterBO();
//RegisterProp objRegisterProp = new RegisterProp();
DataSet dsDetails = new DataSet();
Dictionary<string, string> jsonDictionary = new Dictionary<string, string>();
if (objRegisterProperties.Citizenship <= 0)
{
jsonDictionary.Add("SubmissionStatus", "0");
jsonDictionary.Add("002", "citizenship is required");
return SendData(jsonDictionary);
}
if (objRegisterProperties.LeadRegisterID <= 0)
{
jsonDictionary.Add("SubmissionStatus", "0");
jsonDictionary.Add("002", "LeadRegisterID is required");
return SendData(jsonDictionary);
}
//DataSet objDS = objRegisterBO.VerifyCountryCity(objRegisterProperties);
//if (objDS != null && objDS.Tables[0].Rows.Count > 0 && Convert.ToBoolean(objDS.Tables[0].Rows[0]["Result"]))
//{
objRegisterProperties.LeadStatus = string.Empty;
jsonDictionary = objValidation.ValidateProperties(objRegisterProperties);
if (jsonDictionary.Count > 0)
{
jsonDictionary.Add("SubmissionStatus", "0");
return SendData(jsonDictionary);
}
else
{
//if (objRegisterProperties.DateofBirth.Year > 1900)
//{
objRegisterProperties.RegisterID = intLeadRegisterID;
objRegisterProperties.SPType = "R";
objRegisterProperties.EnteredDate = DateTime.Now;
if (Convert.ToBoolean(objRegisterProperties.FamMngBusiness))
{
objRegisterProperties.MngBusiness = true;
objRegisterProperties.OwnBusiness = false;
objRegisterProperties.OwnCompanyName = string.Empty;
objRegisterProperties.OwnAnnualRevenue = string.Empty;
objRegisterProperties.OwnOffWebsite = string.Empty;
objRegisterProperties.OwnRevCurrencyFormat = 0;
}
else
{
objRegisterProperties.MngBusiness = false;
if (Convert.ToBoolean(objRegisterProperties.FamOwnBusiness))
{
objRegisterProperties.OwnBusiness = true;
objRegisterProperties.FamMngOffWebsite = string.Empty;
objRegisterProperties.RevCurrencyFormat = 0;
objRegisterProperties.AnnualRevenue = string.Empty;
}
else
{
objRegisterProperties.OwnBusiness = false;
objRegisterProperties.OwnCompanyName = string.Empty;
objRegisterProperties.OwnAnnualRevenue = string.Empty;
objRegisterProperties.OwnOffWebsite = string.Empty;
objRegisterProperties.FamMngOffWebsite = string.Empty;
objRegisterProperties.OwnRevCurrencyFormat = 0;
objRegisterProperties.RevCurrencyFormat = 0;
objRegisterProperties.AnnualRevenue = string.Empty;
}
}
intLeadRegisterID = objRegisterBO.AddRegisterDetails(objRegisterProperties);
if ((intLeadRegisterID > 0))
{
jsonDictionary.Add("SubmissionStatus", "1");
return SendData(jsonDictionary);
}
else
{
return ErrorjsonResultData("SubmissionStatus", "0", "010", "Technical Exception");
}
//}
//else
//{
// return ErrorjsonResultData("SubmissionStatus", "0", "052", "Enter valid DateofBirth");
//}
}
//}
//else
//{
// return ErrorjsonResultData("SubmissionStatus", "0", "002", "Please select the city from the displayed options");
//}
}
public Stream submit_reg_data(RegisterProp objRegisterProperties)
{
//RegisterProp objRegisterProperties = new RegisterProp();
//objRegisterProperties = JsonHelper.JsonDeserialize<RegisterProp>(RegisterProperties);
RegisterBO objRegisterBO = new RegisterBO();
//RegisterProp objRegisterProp = new RegisterProp();
DataSet dsDetails = new DataSet();
bool chkStatus = true;
dsDetails = objRegisterBO.FetchRegisterDetailsByEmailIDs(objRegisterProperties);
if (!(dsDetails == null))
{
if ((dsDetails.Tables[0].Rows.Count > 0))
{
chkStatus = false;
}
}
if (chkStatus)
{
Dictionary<string, string> jsonDictionary = new Dictionary<string, string>();
//DataSet objDS = objRegisterBO.VerifyCountryCity(objRegisterProperties);
//if (objDS != null && objDS.Tables[0].Rows.Count > 0 && Convert.ToBoolean(objDS.Tables[0].Rows[0]["Result"]))
//{
objRegisterProperties.LeadStatus = string.Empty;
jsonDictionary.Add("SubmissionStatus", "0");
jsonDictionary = objValidation.ValidateProperties(objRegisterProperties);
if (jsonDictionary.Count > 0)
{
return SendData(jsonDictionary);
}
else
{
jsonDictionary.Clear();
//if (objRegisterProperties.DateofBirth.Year > 1900)
//{
objRegisterProperties.RegisterID = intLeadRegisterID;
objRegisterProperties.SPType = "R";
objRegisterProperties.EnteredDate = DateTime.Now;
if (Convert.ToBoolean(objRegisterProperties.FamMngBusiness))
{
objRegisterProperties.MngBusiness = true;
objRegisterProperties.OwnBusiness = false;
objRegisterProperties.OwnCompanyName = string.Empty;
objRegisterProperties.OwnAnnualRevenue = string.Empty;
objRegisterProperties.OwnOffWebsite = string.Empty;
objRegisterProperties.OwnRevCurrencyFormat = 0;
}
else
{
objRegisterProperties.MngBusiness = false;
if (Convert.ToBoolean(objRegisterProperties.FamOwnBusiness))
{
objRegisterProperties.OwnBusiness = true;
objRegisterProperties.FamMngOffWebsite = string.Empty;
objRegisterProperties.RevCurrencyFormat = 0;
objRegisterProperties.AnnualRevenue = string.Empty;
}
else
{
objRegisterProperties.OwnBusiness = false;
objRegisterProperties.OwnCompanyName = string.Empty;
objRegisterProperties.OwnAnnualRevenue = string.Empty;
objRegisterProperties.OwnOffWebsite = string.Empty;
objRegisterProperties.FamMngOffWebsite = string.Empty;
objRegisterProperties.OwnRevCurrencyFormat = 0;
objRegisterProperties.RevCurrencyFormat = 0;
objRegisterProperties.AnnualRevenue = string.Empty;
}
}
intLeadRegisterID = objRegisterBO.AddRegisterDetails(objRegisterProperties);
if ((intLeadRegisterID > 0))
{
jsonDictionary.Add("SubmissionStatus", "1");
jsonDictionary.Add("LeadRegisterId", intLeadRegisterID.ToString());
return SendData(jsonDictionary);
}
else
{
return ErrorjsonResultData("SubmissionStatus", "0", "010", "Technical Exception");
}
//}
//else
//{
// return ErrorjsonResultData("SubmissionStatus", "0", "052", "Enter valid DateofBirth");
//}
}
//}
//else
//{
// return ErrorjsonResultData("SubmissionStatus", "0", "003", "Please select the city from the displayed options");
//}
}
else
{
return ErrorjsonResultData("SubmissionStatus", "0", "003", "A record with this email or mobile number already exists!!");
}
}
}}
web.config file--->
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<connectionStrings>
<add name="DBConnStr" connectionString="server=inhydxyxertt;database=xyztre;uid=sa;password=123tgr;" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="AppYear" value="2014" />
<add key="ApplYear" value="2014-15" />
<!--<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />-->
</appSettings>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="MFAB.UserServiceBehavior" name="MFAB.MFABService">
<endpoint address="" behaviorConfiguration="MFAB.UserServiceBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="MFAB.IMFABService"/>
<!--<identity>
<dns value="localhost" />
</identity>-->
<!--<endpoint address="/Submit" behaviorConfiguration="MFAB.UserServiceBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="MFAB.IMFABService1"/>-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="MFAB.UserServiceBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MFAB.UserServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
<defaultDocument>
<files>
<add value="MFABService.svc" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
Global.aspx---
namespace MFAB
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(MFABService)));
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
EnableCrossDmainAjaxCall();
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
private void EnableCrossDmainAjaxCall()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin","*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
"GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
"Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
"1728000");
HttpContext.Current.Response.End();
}
}
}
}
I read many tutorials but I could not get this running for my project. I made few GET services which send data and they work great, but I have problems receiving the data. I would really appreciate it if someone could tell me where I fail, instead of posting some links. :)
When I try to call the service in my browser I get the error: Method not allowed. But I Think this is just the first error.
Here is my code.
First the code in Android where I call the service:
public class MainActivity extends Activity {
String SERVICE_URI = "http://10.0.2.2:51602/RestServiceImpl.svc";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpPost request = new HttpPost(SERVICE_URI + "/registerUser");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
try {
JSONStringer user = new JSONStringer()
.object()
.key("userInfo")
.object()
.key("Email").value("mail")
.key("Password").value("pass")
.key("Cauntry").value("country")
.key("UserName").value("username")
.endObject()
.endObject();
StringEntity entity = new StringEntity(user.toString());
request.setEntity(entity);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
IRestServiceImpl.cs:
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "registerUser",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
void receiveData(String data);
}
RestServiceImpl.cs:
public class RestServiceImpl : IRestServiceImpl
{
public void receiveData(String data)
{
//some code
}
}
Web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
<endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Your problem is that you're passing a JSON object to the operation, but the operation is expecting a JSON string. If I understand correctly the Java code, this is what you're sending:
{
"userInfo" : {
"Email" : "mail",
"Password" : "pass",
"Cauntry" : "country",
"UserName" : "username"
}
}
Which is not a JSON string.
There are a couple of things you can do. The first alternative is to modify the operation not to take a string, but to take a data contract which is equivalent to that object.
The code would look something like the one below. Notice that you should also change the body format to Bare (instead of Wrapped).
public class RequestData
{
public UserInfo userInfo { get; set; }
}
public class UserInfo
{
public string Email { get; set; }
public string Password { get; set; }
public string Cauntry { get; set; } // typo to match the OP
public string UserName { get; set; }
}
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "registerUser",
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
void receiveData(RequestData data);
}
Another alternative, if the "schema" of the data changes every time, would be to take the input as a Stream (not a String). That way you should be able to accept basically any input. You may also need a content type mapper. You can find more information about this scenario at http://blogs.msdn.com/b/carlosfigueira/archive/2008/04/17/wcf-raw-programming-model-receiving-arbitrary-data.aspx.
I want to invoke a WCF service using JSONP. There are two methods,
1. "Hello" method, it does not take any paramter. This works fine
2. "Hello1" methods takes a string paramter. This does not work properly. It should return the value which I am passing.
Following is the code snippet
Server code
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace WcfService1
{
[ServiceContract(Namespace = "JsonpAjaxService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string Hello()
{
return "I got the call";
}
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string Hello1(string str)
{
return "Hi, you passed me " + str;
}
}
}
Web config code
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="None" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
</webScriptEndpoint>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="True"
automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
Client code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="JS/JQuery.js" type="text/javascript"></script>
<script type="text/javascript">
var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;
var method;
//Generic function to call WCF Service
function CallService() {
$.ajax({
type: Type, //GET or POST or PUT or DELETE verb
url: Url, // Location of the service
data: Data, //Data sent to server
contentType: ContentType, // content type sent to server
dataType: DataType, //Expected data format from server
processdata: ProcessData, //True or False
success: function (msg) {//On Successfull service call
ServiceSucceeded(msg);
},
error: ServiceFailed// When Service call fails
});
}
function ServiceFailed(result) {
alert('test');
alert('Service call failed: ' + result.status + '' + result.statusText);
Type = null;
Url = null;
Data = null;
ContentType = null;
DataType = null;
ProcessData = null;
}
function Hello() {
var uesrid = "1";
Type = "GET";
Url = "http://localhost:52136/Service1.svc/Hello";
DataType = "jsonp"; ProcessData = false;
method = "Hello";
//debugger;
CallService();
}
function Hello1() {
debugger;
var uesrid = "1";
Type = "GET";
ContentType = "application/json; charset=utf-8";
Url = "http://localhost:52136/Service1.svc/Hello1";
DataType = "jsonp"; //ProcessData = false;
method = "Hello1";
Data = "{'str':'abc'}"; //"{'Input':'a123'}";//"{'StringValue':'1234'}";
//debugger;
CallService();
}
function ServiceSucceeded(result) {
debugger;
if (DataType == "jsonp") {
alert(result); /* Problem, While calling 'Hello1' method I do not get the value in the return string*/
}
}
function ServiceFailed(xhr) {
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
$(document).ready(
function () {
try {
//Hello();
Hello1();
} catch (exception) { }
}
);
</script>
</head>
<body>
<form id="form1" runat="server">
<h1>
JSONP Service Client Page</h1>
</form>
</body>
</html>
Can anybody tell me what is wrong in the code while calling Hello1 method
Atul Sureka
Try passing the data as an object, not as a string:
function Hello1() {
debugger;
var uesrid = "1";
Type = "GET";
ContentType = "application/json; charset=utf-8";
Url = "http://localhost:52136/Service1.svc/Hello1";
DataType = "jsonp"; //ProcessData = false;
method = "Hello1";
Data = {str : 'abc'};
//debugger;
CallService();
}
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace WcfService1
{
[ServiceContract(Namespace = "JsonpAjaxService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string Hello()
{
return "I got the call";
}
[WebGet(BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "SetQuoteEntities?str={str}")]
public string Hello1(string str)
{
return "Hi, you passed me " + str;
}
}
}
When I modified attribute webGet it worked for me.
I'm trying to add message compression to my WCF service, but I'm kind of stuck...
So far I have:
An implementation of MessageEncoder:
public class CompressionEncoder : MessageEncoder
{
public CompressionEncoderFactory _factory;
public const string GZipContentType = "application/x-gzip";
public override string ContentType
{
get
{
return GZipContentType;
}
}
public override string MediaType
{
get { return ContentType; }
}
public override MessageVersion MessageVersion
{
get { return _factory.MessageVersion; }
}
public CompressionEncoder(CompressionEncoderFactory factory)
{
_factory = factory;
}
public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType)
{
var decompressedStream = new GZipStream(stream, System.IO.Compression.CompressionMode.Decompress, false);
XmlReader reader = XmlReader.Create(decompressedStream);
Message msg = Message.CreateMessage(reader, maxSizeOfHeaders, _factory.MessageVersion);
msg.Properties.Encoder = this;
return msg;
}
public override Message ReadMessage(ArraySegment<byte> buffer, BufferManager bufferManager, string contentType)
{
MemoryStream stream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count);
GZipStream decompressedStream = new GZipStream(stream, System.IO.Compression.CompressionMode.Decompress);
return ReadMessage(decompressedStream, int.MaxValue);
}
public override void WriteMessage(Message message, Stream stream)
{
GZipStream compressedStream = new GZipStream(stream, System.IO.Compression.CompressionMode.Compress, true);
XmlWriter writer = XmlWriter.Create(compressedStream);
message.WriteMessage(writer);
}
public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)
{
MemoryStream stream = new MemoryStream();
GZipStream compressedStream = new GZipStream(stream, System.IO.Compression.CompressionMode.Compress, true);
XmlWriter writer = XmlWriter.Create(compressedStream);
message.WriteMessage(writer);
writer.Close();
int messageLength = (int)compressedStream.Length;
byte[] messageBytes = new byte[messageLength];
compressedStream.Read(messageBytes, 0, messageLength);
int totalLength = messageLength + messageOffset;
byte[] totalBytes = bufferManager.TakeBuffer(totalLength);
Array.Copy(messageBytes, 0, totalBytes, messageOffset, messageLength);
ArraySegment<byte> byteArray = new ArraySegment<byte>(totalBytes, messageOffset, messageLength);
return byteArray;
}
}
An implementation of MessageEncoderFactory:
public class CompressionEncoderFactory : MessageEncoderFactory
{
CompressionMessageEncodingBindingElement _element;
public CompressionEncoderFactory(CompressionMessageEncodingBindingElement element)
{
_element = element;
}
public override MessageEncoder Encoder
{
get { return new CompressionEncoder(this); }
}
public override MessageVersion MessageVersion
{
get { return _element.MessageVersion; }
}
}
An implementation of MessageEncodingBindingElement:
public sealed class CompressionMessageEncodingBindingElement : MessageEncodingBindingElement
{
MessageVersion _messageVersion = MessageVersion.None;
public override MessageEncoderFactory CreateMessageEncoderFactory()
{
return new CompressionEncoderFactory(this);
}
public override MessageVersion MessageVersion
{
get
{
return _messageVersion;
}
set
{
_messageVersion = value;
}
}
public override BindingElement Clone()
{
return new CompressionMessageEncodingBindingElement();
}
}
An implementation of BindingElementExtensionElement:
public class CompressionBindingElementExtensionElement : BindingElementExtensionElement
{
public override Type BindingElementType
{
get { return typeof(CompressionMessageEncodingBindingElement); }
}
protected override System.ServiceModel.Channels.BindingElement CreateBindingElement()
{
return new CompressionMessageEncodingBindingElement();
}
}
In the web.config I added this:
<service name="MyService">
<endpoint address="" binding="customBinding" bindingConfiguration="CompressionBinding" contract="IMyService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
...
<bindings>
<customBinding>
<binding name="CompressionBinding">
<messageCompression />
<httpTransport authenticationScheme="Anonymous"></httpTransport>
</binding>
</customBinding>
</bindings>
...
<extensions>
<bindingElementExtensions>
<add name="messageCompression" type="MyNamespace.CompressionBindingElementExtensionElement, MyNamespace" />
</bindingElementExtensions>
</extensions>
But when I run a client and call some service code, I get the following error:
The message version of the outgoing message (EnvelopeNone (http://schemas.microsoft.com/ws/2005/05/envelope/none) AddressingNone (http://schemas.microsoft.com/ws/2005/05/addressing/none)) does not match that of the encoder (Soap12 (http://www.w3.org/2003/05/soap-envelope) Addressing10 (http://www.w3.org/2005/08/addressing)). Make sure the binding is configured with the same version as the message.
What am I doing wrong?
Try changing MessageVersion _messageVersion = MessageVersion.Soap12WSAddressing10; at (3.).
Look at the Microsoft Technology Sample "TechnologySamples\Extensibility\MessageEncoder\Compression\".