Deserialize JSON object sent from Android app to WCF webservice - c#

I'm trying to send a JSON object to my webservice method, the method is defined like this:
public String SendTransaction(string trans)
{
var json_serializer = new JavaScriptSerializer();
Transaction transObj = json_serializer.Deserialize<Transaction>(trans);
return transObj.FileName;
}
Where I want to return the FileName of this JSON string that I got as a parameter.
The code for the android application:
HttpPost request = new HttpPost(
"http://10.118.18.88:8080/Service.svc/SendTransaction");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
JSONStringer jsonString;
jsonString = new JSONStringer()
.object().key("imei").value("2323232323").key("filename")
.value("Finger.NST").endObject();
Log.i("JSON STRING: ", jsonString.toString());
StringEntity entity;
entity = new StringEntity(jsonString.toString(), "UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json"));
entity.setContentType("application/json");
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity httpEntity = response.getEntity();
String xml = EntityUtils.toString(httpEntity);
Log.i("Response: ", xml);
Log.d("WebInvoke", "Status : " + response.getStatusLine());
I only get a long html file out, which tells me The server has encountered an error processing the request. And the status code is HTTP/1.1 400 Bad Request
My Transaction class is defined in C# like this:
[DataContract]
public class Transaction
{
[DataMember(Name ="imei")]
public string Imei { get; set; }
[DataMember (Name="filename")]
public string FileName { get; set; }
}
How can I accomplish this in the right way?
EDIT, this is my web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="httpBehavior">
<webHttp />
</behavior >
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
<services>
<service name="Service.Service">
<endpoint address="" behaviorConfiguration="httpBehavior" binding="webHttpBinding" contract="Service.IService"/>
</service>
</services>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>
<!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />-->
</system.serviceModel>
<system.webServer>
<!-- <modules runAllManagedModulesForAllRequests="true"/>-->
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>

#Tobias, This is not an answer. But since it was a little bit long for comment, I post it here. Maybe it can help to diagnose your problem. [A full working code].
public void TestWCFService()
{
//Start Server
Task.Factory.StartNew(
(_) =>{
Uri baseAddress = new Uri("http://localhost:8080/Test");
WebServiceHost host = new WebServiceHost(typeof(TestService), baseAddress);
host.Open();
},null,TaskCreationOptions.LongRunning).Wait();
//Client
var jsonString = new JavaScriptSerializer().Serialize(new { xaction = new { Imei = "121212", FileName = "Finger.NST" } });
WebClient wc = new WebClient();
wc.Headers.Add("Content-Type", "application/json");
var result = wc.UploadString("http://localhost:8080/Test/Hello", jsonString);
}
[ServiceContract]
public class TestService
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public User Hello(Transaction xaction)
{
return new User() { Id = 1, Name = "Joe", Xaction = xaction };
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public Transaction Xaction { get; set; }
}
public class Transaction
{
public string Imei { get; set; }
public string FileName { get; set; }
}
}

Related

WCF protobuf endpoint 400 bad request

I have WCF service with DataContract json serialization. I would like to add service endpoints to consume Protobuf data messages.
I tried to use nugget package ProtoBuf.Services.WCF.
Added endpoint via web.config configuration. However, every request on protobuf endpoint with address "proto" returns 400 Bad request. Web.config sample is written bellow. Endpoint with default address "" works properly.
Get method:
HTTP 200 OK http://localhost:65460/BeaconService.svc/GetData
HTTP 400 BAD REQUEST: http://localhost:65460/BeaconService.svc/proto/GetData
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding transferMode="Streamed">
<security mode="None" />
</binding>
</webHttpBinding>
<basicHttpBinding>
<binding messageEncoding="Mtom">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<extensions>
<behaviorExtensions>
<add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net" />
</behaviorExtensions>
</extensions>
<services>
<service behaviorConfiguration="DefaultServiceBehavior" name="Services.BeaconService">
<endpoint address="" behaviorConfiguration="httpBehavior" binding="webHttpBinding" contract="Services.IBeaconService" />
<endpoint address="proto" behaviorConfiguration="protoBehavior" binding="basicHttpBinding" contract="Services.IBeaconService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="protoBehavior">
<protobuf />
</behavior>
<behavior name="httpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</system.serviceModel>
Please, which part of configuration is defective. Eventualy, what is proper way to call Get method on "proto" WCF endpoint to avoid HTTP 400 Bad request message?
Unfortunately, I have failed to implement ProtoBuf.Services.WCF and decided to use another approach. In general, WCF by default uses DataContractSerializer.
After reading this article, I realized It is possible to replace this serializer with another one, f.e. protobuf serializer in this library. So I created behavior extension, which replaces DataContractSerializer with my custom ProtobufSerializer. In configuration added another endpoint, which has set behavior extension to use my custom ProtobufSerializer.
WebHttpBehavior:
public class ProtobufBehavior : WebHttpBehavior
{
protected override IDispatchMessageFormatter GetRequestDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
{
return new ProtobufDispatchFormatter(operationDescription);
}
protected override IDispatchMessageFormatter GetReplyDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
{
return new ProtobufDispatchFormatter(operationDescription);
}
}
Dispatch formatter:
namespace Services.Extension.ProtobufSerializationExtension
{
public class ProtobufDispatchFormatter : IDispatchMessageFormatter
{
OperationDescription operation;
bool isVoidInput;
bool isVoidOutput;
public ProtobufDispatchFormatter(OperationDescription operation)
{
this.operation = operation;
this.isVoidInput = operation.Messages[0].Body.Parts.Count == 0;
this.isVoidOutput = operation.Messages.Count == 1 || operation.Messages[1].Body.ReturnValue.Type == typeof(void);
}
public void DeserializeRequest(Message message, object[] parameters)
{
if (!message.IsEmpty)
{
XmlDictionaryReader bodyReader = message.GetReaderAtBodyContents();
bodyReader.ReadStartElement("Binary");
byte[] rawBody = bodyReader.ReadContentAsBase64();
MemoryStream ms = new MemoryStream(rawBody);
using (StreamReader sr = new StreamReader(ms))
for (int i = 0; i < parameters.Length; i++)
parameters[i] = Serializer.Deserialize(operation.Messages[i].Body.Parts[i].Type, sr.BaseStream);
}
}
public Message SerializeReply(MessageVersion messageVersion, object[] parameters, object result)
{
byte[] body;
using (MemoryStream ms = new MemoryStream())
using (StreamWriter sw = new StreamWriter(ms))
{
Serializer.Serialize(sw.BaseStream, result);
sw.Flush();
body = ms.ToArray();
}
Message replyMessage = Message.CreateMessage(messageVersion, operation.Messages[1].Action, new RawBodyWriter(body));
replyMessage.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Raw));
return replyMessage;
}
class RawBodyWriter : BodyWriter
{
internal static readonly byte[] EmptyByteArray = new byte[0];
byte[] content;
public RawBodyWriter(byte[] content)
: base(true)
{
this.content = content;
}
protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
{
writer.WriteStartElement("Binary");
writer.WriteBase64(content, 0, content.Length);
writer.WriteEndElement();
}
}
}
}
Extension element:
namespace Services.Extension.ProtobufSerializationExtension
{
public class ProtobufSerializationServiceElement : BehaviorExtensionElement
{
public override Type BehaviorType
{
get { return typeof(ProtobufBehavior); }
}
protected override object CreateBehavior()
{
return new ProtobufBehavior();
}
}
}
Web config:
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding transferMode="Streamed">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<extensions>
<behaviorExtensions>
<add name="protobufExtension" type="Services.Extension.ProtobufSerializationExtension.ProtobufSerializationServiceElement, Services" />
</behaviorExtensions>
</extensions>
<services>
<service behaviorConfiguration="DefaultServiceBehavior" name="Services.BeaconService">
<endpoint address="" behaviorConfiguration="httpBehavior" binding="webHttpBinding" contract="Services.IBeaconService" />
<endpoint address="proto" behaviorConfiguration="protoBehavior" binding="webHttpBinding" contract="Services.IBeaconService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="protoBehavior">
<webHttp/>
<protobufExtension/>
</behavior>
<behavior name="httpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</system.serviceModel>
Services.Extension.ProtobufSerializationExtension is name of my custom namespace inside application structure. Hope this helps someone.

REST Web service not working,

Hello there i share my code please tell me why my code is not work . I am very new to this REST web service i just started to writing this. so give some solution to the below mention code.
This is my webservices.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
using System.Data;
namespace webservice_PCCFWL
{
[ServiceContract]
public interface IWebServices
{
[OperationContract]
[WebInvoke(UriTemplate = "/get_userauthentication/{username}/{password}",Method ="GET",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json
)]
List<userloginretrive> getuserdetails(string username,string password);
}
[DataContract]
public class userloginretrive
{
public userloginretrive()
{
uname = null;
udesignation = null;
division = null;
ranger = null;
}
[DataMember(Name = "uname", Order = 1)]
public string uname { get; set; }
[DataMember(Name = "udesignation", Order = 2)]
public string udesignation { get; set; }
[DataMember(Name = "division", Order = 3)]
public string division { get; set; }
[DataMember(Name = "ranger", Order = 4)]
public string ranger { get; set; }
}
}
=========================
This is my .cs code
using System;
using System.Collections.Generic;
using System.Web.Security;
using System.Security.Cryptography;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace webservice_PCCFWL
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WebServices" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select WebServices.svc or WebServices.svc.cs at the Solution Explorer and start debugging.
public class WebServices : IWebServices
{
string myKey;
TripleDESCryptoServiceProvider cryptDES3 = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider cryptMD5Hash = new MD5CryptoServiceProvider();
public WebServices()
{
myKey = "kt763g-kj_7gfhd7GJD-563bjf";
}
private string EncryptUser(string myString)
{
cryptDES3.Key = cryptMD5Hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(myKey));
cryptDES3.Mode = CipherMode.ECB;
ICryptoTransform desdencrypt = cryptDES3.CreateEncryptor();
var MyASCIIEncoding = new ASCIIEncoding();
byte[] buff = ASCIIEncoding.ASCII.GetBytes(myString);
return Convert.ToBase64String(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
}
public List<userloginretrive> getuserdetails(string username,string password)
{
List<userloginretrive> objlist = new List<userloginretrive>();
string pass = EncryptUser(password);
try
{
SqlParameter[] para = new SqlParameter[]
{
new SqlParameter("#p_login_id",username),
new SqlParameter("#p_password",pass)
};
using (DataTable dt = SQLHelper.DataAcessLayer.SqlHelper.ExecuteDataTable(sql.Connection.Configuration.ConnectionString, CommandType.Text, "select User_Name,User_Designation,Wl_Division,Wl_Range from UserDetails where User_LoginId=#p_login_id and User_Password=#p_password", para))
{
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
objlist.Add(new userloginretrive
{
uname = Convert.ToString(dr["User_Name"]),
udesignation = Convert.ToString(dr["User_Designation"]),
division = Convert.ToString(dr["Wl_Division"]),
ranger = Convert.ToString(dr["Wl_Range"])
});
}
}
else
{
objlist.Add(new userloginretrive
{
uname = "no",
udesignation = "no",
division = "no",
ranger = "no"
});
}
}
}
catch (Exception ex)
{
throw ex;
}
return objlist;
}
}
}
======================
This is my webconfig file
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
<add key="myConnectionString"
value="Data Source=localhost;Initial Catalog=PCCFWL;uid=sa;pwd=sparc_123;Pooling=false;MINPOOLSIZE=20;MAXPOOLSIZE=1000;"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<httpModules>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
</httpModules>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehaviors">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="serviceBehaviors" name="webservice_PCCFWL.WebServices">
<endpoint address="WebServices.svc" contract="webservice_PCCFWL.IWebServices" binding="webHttpBinding" behaviorConfiguration="web"></endpoint>
<endpoint address="" contract="IMetadataExchange" binding="mexHttpBinding"></endpoint>
</service>
</services>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="ApplicationInsightsWebTracking"/>
<remove name="TelemetryCorrelationHttpModule"/>
<add name="TelemetryCorrelationHttpModule"
type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation"
preCondition="integratedMode,managedHandler"/>
<add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
preCondition="managedHandler"/>
</modules>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
</configuration>
Please give some solution . my program is running the only first page of service opening then nothing happening. so please help me here.

AngularJS , REST, C# Service, 405 - Post Method Not Allowed

I'm trying to do a full Angularjs web site for training. I've read many articles about this Method not allowed, but didn't find a solution. I'm trying to send a data object to my service.
Error : Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
Here is my AngularJS Part.
var addNews =
{
Title: newsList.newsTitle,
NewsContent: newsList.newsContent,
CreationDate: newsList.newsCreationDate,
CreatedBy: newsList.newsAuthor,
ModificationDate: newsList.newsCreationDate,
ModifiedBy: newsList.newsAuthor
};
var news = JSON.stringify(addNews);
$http({
method: 'POST',
dataType: 'json',
url: 'http://localhost:11672/InfinytecServices.svc/SaveNews',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: news
});
Here's my service part
[OperationContract]
[WebInvoke(Method = "POST",
UriTemplate = "/SaveNews",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
int SaveNews(News news);
WebConfig Comming
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.serviceModel>
<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>
<extensions>
<behaviorExtensions>
<add
name="crossOriginResourceSharingBehavior"
type="InfinytecWebService.CORSEnablingBehavior, InfinytecWebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
<crossOriginResourceSharingBehavior />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="serviceBehavior" name="InfinytecWebService.InfinytecServices">
<endpoint address=""
behaviorConfiguration="web"
binding="webHttpBinding"
contract="InfinytecWebService.IInfinytecServices" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
And at least , the CORS
public class CORSEnablingBehavior : BehaviorExtensionElement, IEndpointBehavior
{
public void AddBindingParameters(
ServiceEndpoint endpoint,
BindingParameterCollection bindingParameters){ }
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { }
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
new CORSHeaderInjectingMessageInspector()
);
}
public void Validate(ServiceEndpoint endpoint) { }
public override Type BehaviorType { get { return typeof(CORSEnablingBehavior); } }
protected override object CreateBehavior() { return new CORSEnablingBehavior(); }
private class CORSHeaderInjectingMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(
ref Message request,
IClientChannel channel,
InstanceContext instanceContext)
{
return null;
}
private static IDictionary<string, string> _headersToInject = new Dictionary<string, string>
{
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Request-Method", "POST,GET,PUT,DELETE,OPTIONS" },
{ "Access-Control-Allow-Headers", "X-Requested-With,Content-Type" }
};
public void BeforeSendReply(ref Message reply, object correlationState)
{
var httpHeader = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
foreach (var item in _headersToInject)
httpHeader.Headers.Add(item.Key, item.Value);
}
}
}
Can you help me?
In advance, thanks! :)
Try one of the following
Define a another WebInvoke method with Method="OPTIONS" , the method should return empty content , since your CORS behavior handler adds all pre-flight headers needed for your browser .
Your CORS Extension should analyze the current request METHOD if it is OPTIONS it should short-circuit and return empty content with the pre-flight headers. If the method type is "GET"/"POST" it should let the request through to be processed by the endpoint.
The browser makes two request one to get pre-flight info using options and another for the actual api call .
the problem you are having is the first call by the browser Method=OPTIONS is being rejected by your endpoint. because it can only process POST.

WCF Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata

I have a WebService that intents to upload a file to a server but i keep getting the error in the title when i want to run it from my VS2010.
I have searched over this site and the solutions have not helped me, unless i'm doing something wrong.
this is the site that i get the example: Example
here is my Interface
namespace FUWcf
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class FileUploadService : IFileUploadService
{
public bool UploadFileData(FileData fileData)
{
bool result = false;
try
{
//Set the location where you want to save your file
string FilePath = Path.Combine(ConfigurationManager.AppSettings["Path"], fileData.FileName);
//If fileposition sent as 0 then create an empty file
if (fileData.FilePosition == 0)
{
File.Create(FilePath).Close();
}
//Open the created file to write the buffer data starting at the given file position
using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
fileStream.Seek(fileData.FilePosition, SeekOrigin.Begin);
fileStream.Write(fileData.BufferData, 0, fileData.BufferData.Length);
}
}
catch (Exception ex)
{
ErrorDetails ed = new ErrorDetails();
ed.ErrorCode = 1001;
ed.ErrorMessage = ex.Message;
throw new FaultException<ErrorDetails>(ed);
}
return result;
}
}
}
Here is the service:
namespace FUWcf
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IFileUploadService
{
[OperationContract]
[FaultContract(typeof(ErrorDetails))]
bool UploadFileData(FileData fileData);
}
[DataContract]
public class FileData
{
[DataMember]
public string FileName { get; set; }
[DataMember]
public byte[] BufferData { get; set; }
[DataMember]
public int FilePosition { get; set; }
}
[DataContract]
public class ErrorDetails
{
[DataMember]
public int ErrorCode { get; set; }
[DataMember]
public string ErrorMessage { get; set; }
}
}
And here is my web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<appSettings>
<add key="Path" value="C:\Users\c.asacha\Documents\Proyectos\Generales\FUWcf\FUWcf\temp\"/>
</appSettings>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHBBinding" />
</wsHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="fus" name="FUWcd.FileUploadService">
<endpoint address=""
binding="wsHttpBinding" bindingConfiguration="WSHBBinding" name="FileUploadService"
contract="FUWcf.IFileUploadService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="fus">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
I hope someone can help me with this, or a link for a better example.
Thanks in advance.
You configuration is wrong ( Service name is name="FUWcd.FileUploadService" where it should be name="FUWcf.FileUploadService")
Anyway- Created the web site and added the web service like this ( You dont need Mex endpoint in web configuration as it is already on, The url will become the servicename http://{localServerName}:{port}/FileUploadService.svc):
public class FileUploadService : IFileUploadService
{
public bool UploadFileData(FileData fileData)
{
bool result = false;
try
{
//Set the location where you want to save your file
string FilePath = Path.Combine(ConfigurationManager.AppSettings["Path"], fileData.FileName);
//If fileposition sent as 0 then create an empty file
if (fileData.FilePosition == 0)
{
File.Create(FilePath).Close();
}
//Open the created file to write the buffer data starting at the given file position
using (FileStream fileStream = new FileStream(FilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
fileStream.Seek(fileData.FilePosition, SeekOrigin.Begin);
fileStream.Write(fileData.BufferData, 0, fileData.BufferData.Length);
}
}
catch (Exception ex)
{
ErrorDetails ed = new ErrorDetails();
ed.ErrorCode = 1001;
ed.ErrorMessage = ex.Message;
throw new FaultException<ErrorDetails>(ed);
}
return result;
}
}
Data Contracts were same as well.
Then in Web.Config file added like this and it works fine:
<system.serviceModel>
<services>
<service name="WebApplication1.FileUploadService">
<endpoint address="fileService" binding="wsHttpBinding" bindingConfiguration=""
contract="WebApplication1.IFileUploadService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />

WCF Service service call to stream endpoint fails on 3rd call

quick overview of what I've got here:
WCF Soap service is running over HTTPS, with Message credential of type certificate. I've got 2 endpoints that I use (besides mex), 1 for my normal service calls, the other for streaming files. Here's my web.config for the service (edited some names of items):
<bindings>
<wsHttpBinding>
<binding name="wsHttpEndpointBinding">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="Certificate" />
</security>
</binding>
</wsHttpBinding>
<basicHttpBinding>
<binding name="streamBinding" transferMode="Streamed" messageEncoding="Mtom" maxReceivedMessageSize="2147483646">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="Certificate" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="Services.Behavior" name="MyInterface">
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpEndpointBinding" name="wsHttpEndpoint" contract="IMyInterface" />
<endpoint address="stream" binding="basicHttpBinding" bindingConfiguration="streamBinding" name="streamEndpoint" contract="IMyInterface" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Service.Behavior">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
<serviceCredentials>
<serviceCertificate findValue="CN=Server.Temp.Dev" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
The service has a method, DownloadDocument, that I'm testing. Here's the signature:
DocumentDownloadResponse DownloadDocument(DocumentDownloadRequest request);
It's worth noting the the service throws Exceptions when the data passed in is not valid, and DownloadDocument catches those exceptions and passes back the error message in the response object.
The DocumentDownloadRequest looks like this:
[MessageContract]
public class DocumentDownloadRequest
{
[MessageHeader]
public string SecurityToken { get; set; }
[MessageHeader]
public string LoginName { get; set; }
[MessageHeader]
public string DocumentId { get; set; }
}
And DownloadDocumentResponse:
[MessageContract]
public class DocumentDownloadResponse : ServiceResponse<Stream>
{
public DocumentDownloadResponse()
{
Data = Stream.Null;
}
[MessageHeader(MustUnderstand = true)]
public bool Success { get; set; }
[MessageHeader(MustUnderstand = true)]
public string ErrorMessage { get; set; }
[MessageBodyMember(Order = 1)]
public Stream Data { get; set; }
}
Here's how I call it from the client:
var soapServiceClient = new SoapServiceClient("streamEndpoint");
bool success;
Stream stream;
string errorMessage =
soapServiceClient.DownloadDocument(documentId, loginName, securityToken, out success, out stream);
serviceClient.Close();
Where SecurityToken and LoginName are items that need to validates. What's strange is that from my test client, when I call DownloadDocument with valid data, I am able to download the file perfectly as many times as I want. However, if I pass in an invalid LoginName or SecurityToken, I get error messages indicating incorrect data (as expected). If I pass in invalid data 3 times, however, the client times out. Running the service locally I don't get this issue, everythign runs as expected. Strangely enough, whenI run with fiddler open, I don't get this issue. When I run with the service on my dev server, I have the problems.
The configuration on the dev server matches what I run locally. Using the SvcTraceTool I don't see any errors, except that it only documents the first 2 successful calls, and not the one that failed. It almost makes me think the endpoint just closed itself somehow.
Cliffs:
1) Service with 2 endpoints, one of if which is streaming (the one I'm concerned with).
2) Able to use streaming endpoint to call method to Download files with valid data
3) Service correctly catches bad data 2 times, hangs the 3rd time. No logs in SvcTraceTool, client times out.
Any ideas?
Thanks
In order to answer Rodrigo, I figured I'd post more details:
First off, wrap your generated proxy class in something sort of like this to handle error properly:
public class ProxyWrapper<TServiceClientType, TResultType>
where TServiceClientType : class, ICommunicationObject
{
private static string _endPoint;
public ProxyWrapper(string endPoint = "")
{
_endPoint = endPoint;
}
public TResultType Wrap(Func<string, TServiceClientType> constructWithEndpoint,
Func<TServiceClientType, TResultType> codeBlock)
{
TResultType result = default(TResultType);
TServiceClientType client = default(TServiceClientType);
try
{
client = constructWithEndpoint(_endPoint);
result = codeBlock(client);
client.Close();
}
catch (Exception)
{
if (client != null)
{
client.Abort();
}
throw;
}
return result;
}
}
I then have a client class that wraps around the service calls. Here's the DownloadDocument method:
public MyServiceResponse<Stream> DownloadDocument(string loginName,
string documentId)
{
var proxyWrapper = new MyProxyWrapper<DocumentDownloadResponse>(StreamEndpoint);
DocumentDownloadResponse response =
proxyWrapper.Wrap((client) =>
{
Stream data;
bool success;
string errorMessage = client.DownloadDocument(documentId, loginName,
out success,
out data);
return new DocumentDownloadResponse
{
Data = data,
Success = success,
ErrorMessage = errorMessage
};
});
var result = new MyServiceResponse<Stream>
{
Success = response.Success,
ErrorMessage = response.ErrorMessage
};
if (!response.Success)
{
result.Data = null;
response.Data.Close();
}
else
{
result.Data = response.Data;
}
return result;
}
Note: MyProxyWrapper inherits from ProxyWrapper and specified the WCF client proxy class. Now the actual call looks like this:
var myClient = new MyClient();
var downloadDocumentResponse = myClient.DownloadDocument(someId);
using (
Stream output =
File.OpenWrite(someFilePath))
{
downloadDocumentResponse.Data.CopyTo(output, 2048);
downloadDocumentResponse.Data.Close();
}
Note the two areas that that I call .Close() on the stream, once after writing the file, and once is response.Success == false.

Categories

Resources