Unable to do a 'GET' on WCF Service - wcf

I have a simple WCF contract that contains both 'GET' and 'POST' operations. I have the service running on localhost. I am able to type the service address into my browser and see the service response values. When I try to do the same thing from C# code, I get the error "There was no endpoint listening at...." message. I can however call a 'POST' method on the service from in code.
What am I missing? Below is my Contract code
using System;
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WebServiceTest.Services
{
[ServiceContract]
public interface ITestOne
{
[OperationContract]
[WebInvoke(
Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "GetGreeting/{text1}/{text2}")]
string HelloWorld(string text1, string text2);
[OperationContract]
[WebInvoke(
Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Greet/{text1}")]
string HelloWorld2(string text1);
[OperationContract]
[WebInvoke(
Method="GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "Add/{value1}/{value2}")]
int Add(string value1, string value2);
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
String GetAllSpecies();
[OperationContract]
[WebInvoke(
Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "specie")]
String GetAllSpecies2();
}
}

I found the answer. The problem was that the service was using the service contract ITestOne and the client was using the generated proxy client for ITestOne (obtained through MEX endpoint). The generated proxy did not contain the [WebGet] attribute that the service contract contained.

Related

How to change uritemplate according to the staging enviornment in Restfull wcf service

I am having following operation contract for my non-prod environment
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "members/Empdata")]
but I need to change this uritemplate for my prod environment like below
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "members/Empdata/Search")]
I tried so many things none of them work out
I tried to put a key into web.config but the interface doesn't allow to accept anything from config.
You can only handle this at compile time, not at runtime.
I would really expect a Staging and Production environment does allow for the same url structure with respect to a common root. They are part of the contract.
If you really want to fix this in code then you could use compiler directives and have the two variants in the code governed by providing the correct compile parameters.
[OperationContract]
#if STAGING
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "members/Empdata")]
#endif
#if PRODUCTION
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "members/Empdata/Search")]
#endif
public string Search(string data)
Now you need to make sure you have build configurations for Staging and Production and set the correct Conditional compilation symbols for each.
See also: C# how can I use #if to have different compile result for debug and release?

WCF PUT Service with multiple parameters

I am trying to consume a WCF PUT Service as:
http://dummyurl/EmployeeUpdate?id=99999&item={"var1":true,"var2":1,"var3":1}
Below is the service which is already available (should be a working WCF service)
[WebInvoke(Method = "PUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "EmployeeUpdate/{id}")]
string UpdateEmp(string id, Employee emp);
public string UpdateEmp(string id, Employee emp)
{
try
{
// process data
}
catch (Exception ex)
{
// handle exception
}
return IsSuccess;
}
When I run the service, getting the error message as:
The exception message is 'System.FormatException: Input string was not in a correct format.
I tried to figure out but unable to fix. Found that PUT method will only accept one parameter and the service also defined to receive only one parameter but function is defined with two parameters. I am not understanding how to pass my data as one parameter and how it is resolved in the function
Please provide some guidance on this
Well, this exception can be resolved by 2 way:
1- you must use only one parameter this is due to the UriTemplate :
[WebInvoke(Method = "PUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "EmployeeUpdate/{id}")]
string UpdateEmp(string id, Employee emp);
you must use it like this:
WebInvoke(Method = "PUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "EmployeeUpdate/{id}/{item}")]
string UpdateEmp(string id, Employee item);
I've added /{item} since in the uri you have item instead of emp.
2- You can also create a new object and put your parameter in it
public class ParamClass
{
public string id;
public Employee emp;
}
if you choose this solution you'll have to change your parameter for UpdateEmp accordingly to this change like :
[WebInvoke(Method = "PUT", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "EmployeeUpdate/{emp}")]
string UpdateEmp(ParamClass emp);
also don't forget to change the "EmployeeUpdate" parameter and use in the other part of your archi.

WCF rest service multiple images upload

How can i upload multiple images in WCF rest service.Someone help me to fixed out this issue.
Images receive as Byte array from android device.`
and the requirement is to do a single call from andriod to wcf rest service
Below is my code
[WebInvoke(Method = "POST", UriTemplate = "UploadFilePost?FileName={FileName}&WallPostId={WallPostId}", RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
public MobileFileUploadResponse UploadFilePost(List<Stream> Image, string FileName, long WallPostId)
{
ServicesMapper serviceMapper = new ServicesMapper();
return serviceMapper.UploadFilePost(Image, FileName, WallPostId);
}

Restful WCF Default values

lyDefinition of the resource
[OperationContract]
[WebGet(UriTemplate = "getbydaterange/{insId}/{startDate}/{endDate}", ResponseFormat = WebMessageFormat.Json)]
List<RestfulServiceObj> GetMyObjectsByDateRange(string insId, string startDate, string endDate);
How can i make the last two parameters optional? i.e, I want the bottom three calls to work
"http://domain.com/service.svc/myid/"
"http://domain.com/service.svc/myid/07-07-2011"
"http://domain.com/service.svc/myid/01-01-2011/07-08-2011"
But only the last call works, the rest give a missing parameter error.
Thanks
Bullish
I believe you do so that same way you overload method calls:
[OperationContract]
[WebGet(UriTemplate = "getbydaterange/{insId}", ResponseFormat = WebMessageFormat.Json)]
List<RestfulServiceObj> GetMyObjectsByDateRange(string insId);
[OperationContract]
[WebGet(UriTemplate = "getbydaterange/{insId}/{startDate}", ResponseFormat = WebMessageFormat.Json)]
List<RestfulServiceObj> GetMyObjectsByDateRange(string insId, string startDate);
[OperationContract]
[WebGet(UriTemplate = "getbydaterange/{insId}/{startDate}/{endDate}", ResponseFormat = WebMessageFormat.Json)]
List<RestfulServiceObj> GetMyObjectsByDateRange(string insId, string startDate, string endDate);

WCF RESTful POST

I have a WCF RESTFul service declared thus:
[ServiceContract]
public interface IGasPriceService
{
[OperationContract]
[WebGet
(ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/GetGasPrice/For/ZipCode/{zipCode}"
)]
GasPriceData GetPriceData(string zipCode);
[OperationContract]
[WebGet
(RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/GetGasPrice/For/City/{city}"
)]
GasPriceData GetPriceDataForCity(string city);
[OperationContract]
[WebInvoke
(Method = "POST",
RequestFormat = WebMessageFormat.Xml,
UriTemplate = "/SetGasPrice/For/ZipCode/{zipCode}/Price/{price}"
)]
void SetPriceDataForZipCode(string zipCode, string price);
}
The methods GetPriceData and GetPriceDataforCity work, but the SetPriceDataForZipCode does not work. Can any one let me know why this dows not work.
When I issue a request like:
http://localhost:7002/SetGasPrice/For/ZipCode/45678/7.80
the message that I get is:
EndPoint Not Found
Any ideas how to fix this?
I changed it to
http://localhost:7002/SetGasPrice/For/ZipCode/54568/5.788
and
[OperationContract]
[WebInvoke
(Method = "POST",
RequestFormat = WebMessageFormat.Xml,
UriTemplate = "/SetGasPrice/For/ZipCode/{zipCode}/{price}"
)]
void SetPriceDataForZipCode(string zipCode, string price);
That gives me the message:
Method not allowed.
Any ideas how to resolve this?
Your url needs to be:
http://localhost:7002/SetGasPrice/For/ZipCode/45678/Price/7.80
Or you need to change your template to:
"/SetGasPrice/For/ZipCode/{zipCode}/{price}"
try
UriTemplate = "/SetGasPrice/For/ZipCode/{zipCode}/{dollars}.{cents}"