I am attempting to build a Restful WCF Service which returns data in JSON format. My firsts methods work fine but when I try return a collection my test program receive the next exception:
Unable to write data to the transport connection. An existing connection was forcibly closed by the remote host.
My Service code:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetModes")]
OGetModesResponse OGetModes(OGetModesRequest oGetModes);
}
[DataContract]
public class OGetModesRequest
{
private String m_sTicket;
[DataMember]
public String prTicket
{
get { return m_sTicket; }
set { m_sTicket = value; }
}
}
[DataContract]
public class OGetModesResponse
{
[DataMember]
public string sTicket;
[DataMember]
public emStatus emStatus;
[DataMember]
public IList<CTMode> aoModes;
}
And my test program:
OGetModesRequest oGetModes = new OGetModesRequest { prTicket = sTicket };
ser = new DataContractJsonSerializer(typeof(OGetModesRequest));
mem = new MemoryStream();
ser.WriteObject(mem, oGetModes);
webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
//Exception here
bData = webClient.UploadData("http://localhost:26104/Service.svc/GetModes", "POST", mem.ToArray());
stream = new MemoryStream(bData);
obj = new DataContractJsonSerializer(typeof(OGetModesResponse));
OGetModesResponse OResultModes = obj.ReadObject(stream) as OGetModesResponse;
I debug my services and works fine. What can be happening?
Thanks for help.
Edit (solution):
CTMode is a class used by managing object that I obtain using NHibernate so I create a new class serializable called CMode
[DataContract]
public class OGetModesResponse
{
[DataMember]
public string sTicket;
[DataMember]
public emStatus emStatus;
[DataMember]
public IList<CMode> aoModes;
}
[Serializable]
public class CMode
{
public Int32 nId;
public Int32 nCode;
public String sName;
}
Try to check inner exception and add some logging/ trace on the server.
There are few possibilities for your (generic) error as you may not be aware about inner exception:
object CTMode is missing DataContract, DataMember attribute.
object CTMode is an enum that missing attributes or has incorrect value that cannot be serialized
previous connection is not closed correctly
there is a proxy server on the way and you need to bypassed it
Related
I have created a WCF webservice returning json format data. My code is like below:
String sJSONdata = "";
StreamReader reader = new StreamReader(data);
sJSONdata = reader.ReadToEnd();
//'now convert the JSON into a data table
DataTable dt = GetJSONTable(sJSONdata);
dt.TableName = "Customer";
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach (DataRow rs in dt.Rows)
{
dict = new Dictionary<string, string>();
foreach (DataColumn col in dt.Columns)
{
dict.Add(col.ColumnName, rs[col].ToString());
}
}
return (new JavaScriptSerializer().Serialize(dict));
And I get the following output:
{ "SampleServiceResult":
"{\"Id\":\"1\",\"Name\":\"xyz\",\"email\":\"xya#test.com\"}" }
But I want the output as below:
{ "SampleServiceResult":
{"Id":"1","Name":"xyz","email":"xya#test.com"} }
In the output it adds "\" escape character. How can I remove this and return valid json?
I have tried to replace "\" but it doesn't work.
Thanks
I have got the expected out by applying following changes in my code:
Changed return type from String to Stream
Replaced the below code
return (new JavaScriptSerializer().Serialize(dict));
with
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
return new MemoryStream(Encoding.UTF8.GetBytes(sData));
Thanks everybody for help and support. May it will help someone.
It is better to let WCF take care of the serialization rather then doing it yourself.
You can use WebGet attribute and specify the response format for your operation contract.
Also, as pointed out in the comments, you could return your own .NET class instead of a Dictionary.
The operation contract could be something similar to this:
[OperationContract]
[WebGet(UriTemplate = "/GetCustomer", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
Customer GetCustomer();
with the following data contract:
[DataContract]
public class Customer
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Email { get; set; }
}
You could then implement the operation contract with code similar to this:
public Customer GetCustomer()
{
Customer customer = GetCustomerData(); //Get the data and return a customer object
return customer;
}
I have this interface both in server and client side:
namespace BH_Server {
[ServiceContract]
public interface BHInterface {
[OperationContract]
string GetName( string name );
[OperationContract]
Device GetDevice();
}
[DataContract]
public class Device {
private string dSN;
[DataMember]
public string SN {
get { return dSN; }
set { dSN = value; }
}
}
}
Also, I have this in server side:
public class CronServiceInterface : BHInterface {
public string GetName( string name ) {
return string.Format( "Hello {0}", name );
}
public Device GetDevice() {
Device d = new Device();
d.SN = "123456789";
return d;
}
}
And this on server side, also:
host = new ServiceHost( typeof( CronServiceInterface ), new Uri[] {
new Uri("net.pipe://localhost/")
} );
host.AddServiceEndpoint( typeof( BHInterface ), new NetNamedPipeBinding( NetNamedPipeSecurityMode.None ), "BhPipe" );
host.Open();
To create connection on client side, this code is used:
NetNamedPipeBinding binding = new NetNamedPipeBinding( NetNamedPipeSecurityMode.None );
ChannelFactory<BHInterface> channelFactory = new ChannelFactory<BHInterface>( binding );
EndpointAddress endpointAddress = new EndpointAddress( "net.pipe://localhost/BhPipe/" );
BHInterface iface = channelFactory.CreateChannel( endpointAddress );
Obviously not all the code is written here, I hope it is enough to see what is implemented.
Using Debug.WriteLine( iface.GetName("Tom") ); results "Hello Tom" in client side, but the following code won't work:
Device d;
d = iface.GetDevice();
Debug.WriteLine( string.Format( "Printing sn: {0}", d.SN ) );
It prints: "Printing sn: ".
I'm using .NET 4.5 and error is not thrown. I'm new in WCF topic.
Would somebody so kind explaining to me how could I pass the desired object to client?
To elaborate a bit more... Like the ServiceContract and OperationContract shows the prototypes veses implementation, so goes for the DataContract and DataMembers. You are placing implementation
get { return dSN; }
set { dSN = value; }
Where all is needed is the
public string SN {get;set;}
To solve this just remove the backing field for your property and have DataContract defined as
[DataContract]
public class Device {
[DataMember]
public string SN {get;set;}
}
The reason is that the value of dSN is not sent from the service to the client because it is not a [DataMember]. Other solution would be mark the private field with [DataMember] attribute but you should generally avoid such practice.
Also , remember to update service reference after any change to the data contracts as otherwise client will still see old contracts.
Huh, I found out!
I had to use attributes in my datacontracts!
namespace BH_Server {
[ServiceContract]
public interface BHInterface {
[OperationContract]
string GetName( string name );
[OperationContract]
Device GetDevice();
}
[DataContract( Name = "Device", Namespace = "" )]
public class Device {
[DataMember( Name = "SN", Order = 1 )]
public string SN { get; set; }
}
}
Now it works like a charm!
[DataContract]
public abstract class BusMessage
{
[DataMember(Name = "encoding")]
public string Encoding { get; set; }
[DataMember(Name = "type")]
public virtual MessageType Type
{
get { return _type; }
private set { _type = value; }
}
}
[DataContract]
public class BusTextMessage : BusMessage
{
[DataMember(Name = "type")]
public override MessageType Type
{
get { return MessageType.Text; }
}
[DataMember(Name = "payload")]
public string Payload { get; set; }
}
[ServiceContract]
[ServiceKnownType("GetKnownTypes", typeof(Helper))]
public interface ICommunicationService
{
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/SendMessage")]
string SendMessage(BusMessage jsonMessage);
}
}
When I send request with Postman chrome, if I don't add __type as "__type":"BusTextMessage:#TransportModels.Messages" the object won't be serialized properly because it doesn't know how to instantiate BusMessage class. I have already defined Type property which defines the type of message. Is there any possibility to override __type behaviour for example return proper implementation depending on Type property? I don't want anyone to put __type information to json manually so is there an option to edit json before deserialization and add __type property manually to json if it doesn't exist? For example I want to do something like this:
public void BeforeDeserialization(string json)
{
if(json doesnt include __type)
{
if(json property type(my property) is MessageType.Text)
add to json "__type":"BusTextMessage:#TransportModels.Messages"
///etc
}
}
I Found this methods but it doesn't seem to be usable:
[OnDeserializing()]
internal void OnDeserializingMethod(StreamingContext context)
{
}
I think you need to add the KnownType attribute to the BusMessage class.
[DataContract]
[KnownType(typeof(BusTextMessage)]
public class BusMessage
{
.
.
.
}
This is the quickest solution I discovered. I configure MessageInspector and handle AfterReceiveRequest. Then I check message format(XML,JSON). If it is XML(for example sent from any WCF Client written in C#, WCF is configured to send everything with XML's) then I accept that message because field __type will be automatically inserted by WCF mechanism. Otherwise I Check if it is JSON, for example sent from external client. If it doesn't contain property "__type" I check my property Type and generate proper __type value. For example if my Type is equal to Text I add __type property BusTextMessage:#TransportModels.Messages and insert it into JSON and then recreate the message. I couldn't find quicker and easier solution and it seems to be working. Handling AfterReceiveRequest I found at http://code.msdn.microsoft.com/windowsdesktop/WCF-REST-Message-Inspector-c4b6790b.
public class MessageTypeInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
RecreateMessage(ref request);
return null;
}
}
private void RecreateMessage(ref Message message)
{
WebContentFormat messageFormat = this.GetMessageContentFormat(message);
var ms = new MemoryStream();
XmlDictionaryWriter writer = null;
switch (messageFormat)
{
case WebContentFormat.Default:
case WebContentFormat.Xml:
writer = XmlDictionaryWriter.CreateTextWriter(ms);
break;
case WebContentFormat.Json:
writer = JsonReaderWriterFactory.CreateJsonWriter(ms);
break;
case WebContentFormat.Raw:
this.ReadRawBody(ref message);
break;
}
message.WriteMessage(writer);
writer.Flush();
string messageBody = Encoding.UTF8.GetString(ms.ToArray());
if (messageFormat == WebContentFormat.Json && !messageBody.Contains("__type"))
messageBody = AddTypeField(messageBody);
ms.Position = 0;
ms = new MemoryStream(Encoding.UTF8.GetBytes(messageBody));
XmlDictionaryReader reader = messageFormat == WebContentFormat.Json ?
JsonReaderWriterFactory.CreateJsonReader(ms, XmlDictionaryReaderQuotas.Max) :
XmlDictionaryReader.CreateTextReader(ms, XmlDictionaryReaderQuotas.Max);
Message newMessage = Message.CreateMessage(reader, int.MaxValue, message.Version);
newMessage.Properties.CopyProperties(message.Properties);
message = newMessage;
}
private WebContentFormat GetMessageContentFormat(Message message)
{
WebContentFormat format = WebContentFormat.Default;
if (message.Properties.ContainsKey(WebBodyFormatMessageProperty.Name))
{
WebBodyFormatMessageProperty bodyFormat;
bodyFormat = (WebBodyFormatMessageProperty)message.Properties[WebBodyFormatMessageProperty.Name];
format = bodyFormat.Format;
}
return format;
}
private string AddTypeField(string jsonReply)
{
var typeRegex = new Regex("\"type\":(?<number>[0-9]*)");
Match match = typeRegex.Match(jsonReply);
if (match.Success)
{
int number = Int32.Parse(match.Groups["number"].Value);
var type = (MessageType)number;
var nameFormat = string.Format("Bus{0}Message", type);
string format = string.Format("\"__type\":\"{0}:#TransportModels.Messages\"", nameFormat);
jsonReply = "{" + string.Format("{0},{1}", format, jsonReply.Substring(1));
return jsonReply;
}
else
{
throw new Exception("Wrong message type.");
}
}
Hi,
I have 2 clients with 2 different servers.
After generating wsdl classes I change url address for clients accordingly in SoapHttpClientProtocol consructor.
from
this.Url = "http://10.0.3.5:88/SomeName/dish
to
this.Url = "http://192.168.20.5:88/SomeOtherName/dish
But I can't change SoapDocumentMethodAttribute at runtime. Without changing it my method doesn't return DataSet just null. After changing all addresses in attribute everything works fine.
[System.Web.Services.Protocols.SoapDocumentMethodAttribute( "http://10.0.3.5:88/SomeName/EuroSoft/ProductTransferExecute", RequestNamespace = "http://10.0.3.5:88/SomeName/dish", ResponseNamespace = "http://10.0.3.5:88/SomeName/dish", Use = System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle =
System.Web.Services.Protocols.SoapParameterStyle.Wrapped )]
public System.Data.DataSet ProductTransferExecute( [System.Xml.Serialization.XmlElementAttribute( IsNullable = true )] string department, [System.Xml.Serialization.XmlElementAttribute( IsNullable = true )] string XMLproducts, out int sqlcode ) {}
Services are generated by Sybase Anywhere 9 database. Is it possible to change it dynamic? What needs to be identical for this to work?
Create a CustomSoapHttpClientProtocol:
public class CustomSoapHttpClientProtocol : SoapHttpClientProtocol
{
public string SoapActionUrl { get; private set; }
public CustomSoapHttpClientProtocol(string soapActionUrl)
{
this.SoapActionUrl = soapActionUrl;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
const string soapAction = "SOAPAction";
if (request.Headers.Count > 0 && request.Headers.AllKeys.Contains(soapAction))
{
request.Headers[soapAction] = SoapActionUrl;
}
WebResponse response = base.GetWebResponse(request);
return response;
}
Then in your proxy class replace SoapHttpClientProtocol with your CustomSoapHttpClientProtocol.
WCF offers two options for ResponseFormat attribute in WebGet annotation in ServiceContract.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "greet/{value}", BodyStyle = WebMessageBodyStyle.Bare)]
string GetData(string value);
[OperationContract]
[WebGet(UriTemplate = "foo", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
string Foo();
The options for ResponseFormat are WebMessageFormat.Json and WebMessageFormat.Xml. Is it possible to write my own web message format? I would like that when client calls foo() method he gets raw string - without json or xml wrappers.
Try using
BodyStyle = WebMessageBodyStyle.Bare
Then return a System.IO.Stream from your function.
Here's some code I use to return an image out of a database, but accessible via a URL:
[OperationContract()]
[WebGet(UriTemplate = "Person/{personID}/Image", BodyStyle = WebMessageBodyStyle.Bare)]
System.IO.Stream GetImage(string personID);
Implementation:
public System.IO.Stream GetImage(string personID)
{
// parse personID, call DB
OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse;
if (image_not_found_in_DB)
{
context.StatusCode = System.Net.HttpStatusCode.Redirect;
context.Headers.Add(System.Net.HttpResponseHeader.Location, url_of_a_default_image);
return null;
}
// everything is OK, so send image
context.Headers.Add(System.Net.HttpResponseHeader.CacheControl, "public");
context.ContentType = "image/jpeg";
context.LastModified = date_image_was_stored_in_database;
context.StatusCode = System.Net.HttpStatusCode.OK;
return new System.IO.MemoryStream(buffer_containing_jpeg_image_from_database);
}
In your case, to return a raw string, set the ContentType to something like "text/plain" and return your data as a stream. At a guess, something like this:
return new System.IO.MemoryStream(ASCIIEncoding.Default.GetBytes(string_to_send));
WebGetAttribute is shipped by Microsoft, and I don't think you can extend WebMessageFormat. However you could probably extend the WebHttpBinding that uses WebGetAttribute. You could add your own attribute like
[WebGet2(UriTemplate = "foo", ResponseFormat = WebMessageFormat2.PlainText)]
string Foo();
In general, customizing the message layout in WCF is called custom message encoder/encoding. Microsoft provides an example: Custom Message Encoder: Compression Encoder. Also another common extension people do is to extend behavior to add custom error handling, so you could look for some example in that direction.
I implemented this attribute like this, maybe it will help someone in the future:
[AttributeUsage(AttributeTargets.Method)]
public class WebGetText : Attribute, IOperationBehavior
{
public void Validate(OperationDescription operationDescription)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.Formatter = new Formatter(dispatchOperation.Formatter);
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
}
}
public class Formatter : IDispatchMessageFormatter
{
IDispatchMessageFormatter form;
public Formatter (IDispatchMessageFormatter form)
{
this.form = form;
}
public void DeserializeRequest(Message message, object[] parameters)
{
form.DeserializeRequest(message, parameters)
}
public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
{
IEnumerable<object> cl = (IEnumerable<object>)result;
StringBuilder csvdata = new StringBuilder();
foreach (object userVariableClass in cl) {
Type type = userVariableClass.GetType();
PropertyInfo[] fields = type.GetProperties();
// Dim header As String = String.Join(";", fields.Select(Function(f) f.Name + ": " + f.GetValue(userVariableClass, Nothing).ToString()).ToArray())
// csvdata.AppendLine("")
// csvdata.AppendLine(header)
csvdata.AppendLine(ToCsvFields(";", fields, userVariableClass));
csvdata.AppendLine("");
csvdata.AppendLine("=====EOF=====");
csvdata.AppendLine("");
}
Message msg = WebOperationContext.Current.CreateTextResponse(csvdata.ToString());
return msg;
}
public static string ToCsvFields(string separator, PropertyInfo[] fields, object o)
{
StringBuilder linie = new StringBuilder();
foreach (PropertyInfo f in fields) {
if (linie.Length > 0) {
}
object x = f.GetValue(o, null);
if (x != null) {
linie.AppendLine(f.Name + ": " + x.ToString());
} else {
linie.AppendLine(f.Name + ": Nothing");
}
}
return linie.ToString();
}
}
There is one way how to achieve this if you're dealing with HTTP, it's not exactly nice, but I thought I could mention it.
You can set the return type of your method to void and just output your raw string directly into the response.
[OperationContract]
[WebGet(UriTemplate = "foo")]
void Foo()
{
HttpContext.Current.Response.Write("bar");
}