WCF rest service multiple images upload - wcf

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);
}

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 to WCF communication 401, HttpClient

I have a WCF REST service that needs to communicate with another WCF REST service.
There are three websites:
Default Web Site
Website1
Website2
If I set up both services in Default Web Site and connect to the other (using HttpClient) using the URI http://localhost/service then everything is okay.
The desired set-up is to move these two services to separate websites and rather than using the URI http://localhost/service, accessing the service via http://website1.domain.com/service still using HttpClient.
I received the exception:
System.ArgumentOutOfRangeException: Unauthorized (401) is not one of
the following: OK (200), Created (201), Accepted (202),
NonAuthoritativeInformation (203), NoContent (204), ResetContent
(205), PartialContent (206)
I can see this is a 401, but what is going on here?
Thanks
I think this is related to your setup for webservice. It is best if you just create GET,POST,Put,DELETE heartbeat calls for new services and then check those from fiddler. If you get 401, it may mean your app pool identity could not access something.
Steps to fix that:
Give user read/write/modify/execute/..similar rights at your WCF publish folder
Create app pool for this site in .net 4 integrated
Set this user to application pool identity, enable anonymous mode
Enable PUt,Delete verbs as well
Part of a heartbeat class in your service to test calls:
[DataContract]
public class StatusInfo
{
[DataMember]
public string MachineName { get; set; }
[DataMember]
public string IpAddress{ get; set; }
[DataMember]
public string Methodname { get; set; }
public override string ToString()
{
return "Machinename:" + MachineName + " ;IP:" + IpAddress + "; Method:" + Methodname;
}
}
private void ResolveStatus(StatusInfo statusInfo,string methodname)
{
try
{
var context = System.ServiceModel.OperationContext.Current;
RemoteEndpointMessageProperty property =
(RemoteEndpointMessageProperty)
context.IncomingMessageProperties[RemoteEndpointMessageProperty.Name];
statusInfo.IpAddress = property.Address;
statusInfo.MachineName = Environment.MachineName;
statusInfo.Methodname = methodname;
}catch(Exception ex)
{
}
}
/// <summary>
/// create task
/// </summary>
/// <param name="taskwrapped"></param>
[WebInvoke(Method = "POST", UriTemplate = "", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public StatusInfo postcall()
{
StatusInfo statusInfo = new StatusInfo();
logger.Trace(Tagname + "postcall");
ResolveStatus(statusInfo, "POST");
return statusInfo;
}
/// <summary>
/// edit task
/// </summary>
[WebInvoke(Method = "PUT", UriTemplate = "", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public StatusInfo Edit()
{
StatusInfo statusInfo = new StatusInfo();
logger.Trace(Tagname + "Edit");
ResolveStatus(statusInfo, "PUT");
return statusInfo;
}
//delete request with taskid
[WebInvoke(Method = "DELETE", UriTemplate = "", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public StatusInfo DeleteCall()
{
StatusInfo statusInfo = new StatusInfo();
logger.Trace(Tagname + "Edit");
ResolveStatus(statusInfo, "DELETE");
return statusInfo;
}
//delete request with taskid
[WebInvoke(Method = "DELETE", UriTemplate = "/{recordid}", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public StatusInfo DeleteCallWithParam(string recordid)
{
StatusInfo statusInfo = new StatusInfo();
logger.Trace(Tagname + "Edit");
ResolveStatus(statusInfo, "DELETE/"+recordid);
return statusInfo;
}
enter code here
I received the exception:
Who is "I"? One of the web services or some other client?
If I'm understanding things correctly, it's the receiving end that seems to be expecting a range of responses, 401 not being one of them. It maybe some error checking code that expects "this range" of responses and does X (and 401 isn't one of these, or there is no "default" method to account for x response?).
That said, 401, is an authorization error so check on possible ServiceAuthorizationManager and/or similar settings in place that isn't being met by "I" causing the 401 response in the first place....
Hth...
My guess is you are missing authorizaton headers or credentials.
Check this out :
Consume RESt API from .NET
How to authenticate with Rest-client based on HttpClient and .net4

Unable to do a 'GET' on WCF Service

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.

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}"