WCF REST WebHttpBinding cookie handling - wcf

The problem is WCF client doesn't respect server cookie (doesn't set it on the next request). Here is how I create the client:
WebChannelFactory<IPostService> factory = new WebChannelFactory<IPostService>(
new WebHttpBinding() {AllowCookies = true},
new Uri("http://10.6.90.45:8081"));
_proxy = factory.CreateChannel();
Settings AllowCookies to false has no effect too.
What I did for now is wrote a simple IClientMessageInspector to persist the server cookie between requests, but I really don't want to do this, there should be a way to handle cookie in a standard way.
The custom inspector I'm using now, which is working as expected, but I'm looking for a "clean" solution:
public class CookieInspector : IClientMessageInspector
{
private string _cookie;
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
if(_cookie != null && request.Properties.ContainsKey("httpRequest"))
{
HttpRequestMessageProperty httpRequest = request.Properties["httpRequest"] as HttpRequestMessageProperty;
if(httpRequest != null)
{
httpRequest.Headers["Cookie"] = _cookie;
}
}
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
if (reply.Properties.ContainsKey("httpResponse"))
{
HttpResponseMessageProperty httpResponse = reply.Properties["httpResponse"] as HttpResponseMessageProperty;
if(httpResponse != null)
{
string cookie = httpResponse.Headers.Get("Set-Cookie");
if (cookie != null) _cookie = cookie;
}
}
}
}

How to add cookie on a HttpTransportBindingElement
see this page it has complete answer to your question

Don't know if this was "fixed" in more recent .NET version (I'm on 4.5), or if the OP had other problems that were keeping this from working, but the allowCookies setting DOES work for this purpose.
It is working for me.... I also have "aspnetcompatabilitymode" set on the server.
<webHttpBinding>
<binding name="webHttpEndpointBinding" allowCookies="true">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows"/>
</security>
</binding>
</webHttpBinding>

Related

WCF Timeout exception on streaming big files

I'm currently working on a WCF streaming service. So far everything works great for files up to 2 GB. I've set up the service as a streaming service and I am chunking the files on my own on 5 MB chunks. However, files bigger than 2 GB (somewhere there is the threshold) i always get an InvalidOperationExceptionwith the message Timeouts are not supported on this stream. I'm not really sure why and where this exception is thrown. It don't think this is a server side problem because every request should be the same and most of them work. But the exceptions comes from the generated proxy. So the source is System.Private.ServiceModel
Stack trace:
at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.End(SendAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass0.<CreateGenericTask>b__1(IAsyncResult asyncResult)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Company.OurApp.App.DataService.BaseFile.<DownloadItem>d__59.MoveNext()
Here is my server implementaion:
var response = new GetFileResponse();
using (var impersonation = new Impersonation(request.Domain, request.Username, request.Password))
{
using (Stream fStream = File.OpenRead(request.FullFilePath))
{
fStream.Seek(request.FilePart * request.FilePartSize, SeekOrigin.Begin);
BinaryReader bStream = new BinaryReader(fStream);
var filePart = bStream.ReadBytes(request.FilePartSize);
using (Stream mStream = new MemoryStream(filePart))
{
response.FileByteStream = mStream;
return response;
}
}
}
The GetFileResponse looks like this:
[MessageContract]
public class GetFileResponse
{
[MessageBodyMember(Order = 1)]
public Stream FileByteStream { get; set; }
}
This is how the client handles the download (UWP App):
using (Stream f = await StorageFile.OpenStreamForWriteAsync())
{
//Cancelation area - after every async operation if possilble
for (int i = 0; i < sections; i++)
{
token.ThrowIfCancellationRequested();
var response = await client.GetFilePartAsync(request.ConnectionPassword, request.Domain, i, FilePartSize, FullPath, request.Password, request.Username);
token.ThrowIfCancellationRequested();
DownloadProgress = response.FileByteStream.Length;
f.Seek(0, SeekOrigin.End);
await f.WriteAsync(response.FileByteStream, 0, response.FileByteStream.Length);
await f.FlushAsync();
}
}
And here is the service web.config:
<system.serviceModel>
<services>
<service behaviorConfiguration="HttpsServiceBehaviour"
name="Company.OurApp.TransportService.DataService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="streamedBinding" contract="Company.OurAppTransportService.IDataService">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="HttpsServiceBehaviour">
<!-- 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="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="streamedBinding" transferMode="Streamed" closeTimeout="10:00:00">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
When generating the client proxy, i set some timeouts but that didn't change anything:
public DataServiceClient GetDataServiceClient(string endpoint = null)
{
var useEndpoint = String.IsNullOrEmpty(endpoint) ? Configuration.ConfigService : endpoint;
System.ServiceModel.BasicHttpBinding result = new System.ServiceModel.BasicHttpBinding();
result.MaxBufferSize = int.MaxValue;
result.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
result.MaxReceivedMessageSize = int.MaxValue;
result.AllowCookies = true;
result.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
//TODO Try to work with timeouts for larges files?
result.SendTimeout = TimeSpan.FromMinutes(5);
result.ReceiveTimeout = TimeSpan.FromMinutes(5);
result.OpenTimeout = TimeSpan.MaxValue;
if (useEndpoint.ToLower().StartsWith("https://"))
result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
else
result.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly;
var client = new DataServiceClient(result, new System.ServiceModel.EndpointAddress(String.Concat(useEndpoint, fixedEndpointSuffix)));
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
if (AppState.IsLoggedIn)
{
client.ClientCredentials.Windows.ClientCredential.UserName = $#"{AppState.Domain}\{AppState.User}";
client.ClientCredentials.Windows.ClientCredential.Password = AppState.Password;
}
return client;
}
Any idea where and why the exception is thrown? Server? Client? Is it coming from the stream? Help is very much appreciated.
For everybody else facing the same problem. I got it solved by analyzing the exceptions with the WCF TraceViewer. I also called the Service from a console application to be sure it's not a UWP problem. The problem was that I closed the streams before the response could reach the client.
Broken implementation:
var response = new GetFileResponse();
using (var impersonation = new Impersonation(request.Domain, request.Username, request.Password))
{
using (Stream fStream = File.OpenRead(request.FullFilePath))
{
fStream.Seek(request.FilePart * request.FilePartSize, SeekOrigin.Begin);
BinaryReader bStream = new BinaryReader(fStream);
var filePart = bStream.ReadBytes(request.FilePartSize);
using (Stream mStream = new MemoryStream(filePart))
{
response.FileByteStream = mStream;
return response;
}
}
}
This one fixed it for me:
Stream fStream = File.OpenRead(request.FullFilePath);
long offset = request.FilePart * request.FilePartSize;
fStream.Seek(offset, SeekOrigin.Begin);
BinaryReader bStream = new BinaryReader(fStream);
var filePart = bStream.ReadBytes((int)request.FilePartSize);
Stream mStream = new MemoryStream(filePart);
response.FileByteStream = mStream;
return response;
Hope it helps!

WCF service proxy doesn't refresh binding settings from config file

My application changes end-point settings at run-time and persists the changes to config file. But when I create a new service proxy instance, the end-point settings are the ones that were before the update. How do I force the proxy to get new settings?
You will need to detect if your configuration file has been updated be it your main app.config / web.config or any external configuration file you are using via configSource.
If a change is detected you will need to refresh the system.serviceModel configuration section:
ConfigurationManager.RefreshSection("system.serviceModel/client");
Existing Channels and ChannelFactories will not pick up the changes so they will need to be closed and new ones created.
The following example shows this in action:
[TestFixture]
class When_trying_to_change_service_endpoints_on_the_fly
{
[Test]
public void Should_use_the_new_endpoint()
{
var someService1 = Substitute.For<ISomeWebService>();
var someService2 = Substitute.For<ISomeWebService>();
var serviceHost1 = CreateServiceHost(someService1, new Uri("http://localhost:50001"));
var serviceHost2 = CreateServiceHost(someService2, new Uri("http://localhost:50002"));
serviceHost1.Open();
serviceHost2.Open();
UpdateEndpointInConfig(new Uri("http://localhost:50001"));
var channelFactory = new ChannelFactory<ISomeWebService>("TestReloadConfig");
var channel1 = channelFactory.CreateChannel();
channel1.ServiceMethod();
((IChannel)channel1).Close();
channelFactory.Close();
UpdateEndpointInConfig(new Uri("http://localhost:50002"));
channelFactory = new ChannelFactory<ISomeWebService>("TestReloadConfig");
var channel2 = channelFactory.CreateChannel();
channel2.ServiceMethod();
((IChannel)channel2).Close();
serviceHost1.Close();
serviceHost2.Close();
someService1.Received(1).ServiceMethod();
someService2.Received(1).ServiceMethod();
}
private static void UpdateEndpointInConfig(Uri endpointAddress)
{
var configFile = new ExeConfigurationFileMap();
configFile.ExeConfigFilename = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath;
var config = ConfigurationManager.OpenMappedExeConfiguration(configFile, ConfigurationUserLevel.None);
var serviceModelConfig = (ServiceModelSectionGroup) config.GetSectionGroup("system.serviceModel");
serviceModelConfig.Client.Endpoints[0].Address = endpointAddress;
config.Save();
ConfigurationManager.RefreshSection("system.serviceModel/client");
}
private ServiceHost CreateServiceHost<TService>(TService service, Uri baseUri)
{
var serviceHost = new ServiceHost(service, new Uri[0]);
serviceHost.Description.Behaviors.Find<ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
serviceHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single;
serviceHost.AddServiceEndpoint(typeof(TService), new BasicHttpBinding(), baseUri);
return serviceHost;
}
}
[ServiceContract]
public interface ISomeWebService
{
[OperationContract]
void ServiceMethod();
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:50000" binding="basicHttpBinding" contract="Junk.ISomeWebService" name="TestReloadConfig" />
</client>
</system.serviceModel>
</configuration>
If you managed the endpoint configurations another way you could update any ChannelFactory instance manually as you have access to the Endpoint and Binding properties.

POST request to the WCF service by the use of HttpWebRequest

My Service Contract
[ServiceContract]
public interface ITsdxService
{
[OperationContract]
[WebGet(UriTemplate="/GetTestCostCentre")]
CostCentre GetTestCostCentre();
[OperationContract]
[WebInvoke(UriTemplate="/SetCostCentre", Method="POST")]
string SetCostCentre(CostCentre cc);
}
public class TsdxService : ITsdxService
{
public CostCentre GetTestCostCentre()
{
CostCentre cc = new CostCentre();
cc.Code = "Test";
cc.Name = "Test Cost Centre";
cc.Description = new byte[] { 12, 34, 89, 240, 66, 87, 189 };
cc.SAPStatus = "Existent";
cc.SAPSiteFolder = "Folder1";
return cc;
}
public string SetCostCentre(CostCentre cc)
{
return cc.Code;
}
}
Then I start this service and try to work with it from different application:
Uri requestUri = new Uri(textBox1.Text + "/tsdx/GetTestCostCentre");
HttpWebRequest request = WebRequest.Create(requestUri) as HttpWebRequest;
XElement root;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
root = XElement.Parse(reader.ReadToEnd());
textBox2.Text = root.ToString();
}
All is ok, I am getting the xml document. But I've faced with problem when I tried to send POST request to this service:
Uri requestUri = new Uri(textBox1.Text + "/tsdx/SetCostCentre");
HttpWebRequest request = WebRequest.Create(requestUri) as HttpWebRequest;
byte[] bytes = Encoding.UTF8.GetBytes(textBox2.Text);
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream dataStream = request.GetRequestStream();
dataStream.Write(bytes, 0, bytes.Length);
dataStream.Close();
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
textBox2.Text = reader.ReadToEnd();
}
Exception: The remote server returned an error: (400) Bad Request.
What I did wrong?
Change your client code like this
byte[] bytes = Encoding.UTF8.GetBytes(#"<CostCentre xmlns=""http://schemas.datacontract.org/2004/07/WCF_BadRequestService"">
<Code>String content</Code>
<Description>QmFzZSA2NCBTdHJlYW0=</Description>
<Name>String content</Name>
<SAPSiteFolder>String content</SAPSiteFolder>
<SAPStatus>String content</SAPStatus>
</CostCentre>");
request.ContentLength = bytes.Length;
request.Method = "POST";
request.ContentType = "application/xml";
Now it's fine.
And also i think Java supports WCF BasicHttpBinding, and you can use WCF services using the Java provided tools to generate web-service proxy in an easy supported manner.
Another thing you might want to do is to extend ClientBase to do all the hard work of serializing xml for you. Especially if you want to support more than one message format like json it will make your life a lot easier plus it will make any changes to the interface compile time errors instead of runtime errors.
public class ITsdxServiceProxy : ClientBase<ITsdxService>, ITsdxService {
#region ITsdxService Members
public CostCentre GetTestCostCentre() {
return Channel.GetTestCostCentre();
}
public string SetCostCentre(CostCentre cc) {
return Channel.SetCostCentre(cc);
}
#endregion
}
usage on client side
var proxy = new ITsdxServiceProxy();
var costCenter = proxy.GetTestCostCentre();
Configuration on client side
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint
address="Root address for rest service"
binding="webHttpBinding"
behaviorConfiguration="web"
contract="FullyQualifiedNameOfInterface.ITsdxService">
</endpoint>
</client>
</system.serviceModel>

Authorization Header is missing in Http request using WCF

I am accessing a web service using WCF. Using WSHttpBinding, Security mode is set Transport (https) and client credential type is Basic. When I try to access the service using the proxy, getting an 401 unauthorized exception.
Here is the Binding
var binding = new WSHttpBinding()
{
UseDefaultWebProxy = true,
Security =
{
Mode = SecurityMode.Transport,
Transport =
{
ClientCredentialType = HttpClientCredentialType.Basic,
},
}
};
Here is the service call
var client = new InternetClient(binding, new EndpointAddress("httpsurl"));
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";
client.ProcessMessage("somevalue");
When looked into Http headers using Http Analyzer
CONNECT HEADER
(Request-Line):CONNECT somehost.com:443 HTTP/1.1
Host:somehost.com
Proxy-Connection:Keep-Alive
POST HEADER
(Request-Line):POST /Company/1.0 HTTP/1.1
Content-Type:application/soap+xml; charset=utf-8
VsDebuggerCausalityData:uIDPo+voStemjalOv5LtRotFQ7UAAAAAUKLJpa755k6oRwto14BnuE2PDtYKxr9LhfqXFSOo8pEACQAA
Host:somehost.com
Content-Length:898
Expect:100-continue
Connection:Keep-Alive
If you see the header Authorization header is missing
Now my question is why WCF call missing the Authorization header? Am I missing something? . Please ask if you need more information
This is a common problem, but the situation is different from what you think.
It turns out that initially for the 1st request a WCF client that is configured to use HTTP basic authentication will nevertheless send the request without the necessary Authorization header to the server. This is the default behavior of the HttpWebRequest class used by the WCF client.
Normally, the web service server will then return a HTTP 401 Unauthorized response to the WCF client, upon which the latter will resend the message with the Authorization header. This means under normal conditions for HTTP Basic Authentication there will be a a rather useless round trip to the server.
This also explains why the header was missing in your sniffed message. Some Http sniffs possibly don't pass on the 401 response, so the whole exchange gets messed up.
The server round-trip and dependence on the 401 response can be avoided by manually injecting the required Authorization header into every request. See e.g. how to manually inject Authorization header into WCF request
As a slight modification from a previous answer, to support async / await calls, you can actually create a new OperationContext and pass it around on whatever thread you like (as long as it is not shared across concurrent threads as it isn't a thread-safe object)
var client = new MyClient();
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" + client.ClientCredentials.UserName.Password));
var context = new OperationContext(ormClient.InnerChannel);
using (new OperationContextScope(context))
{
context.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
return await client.SomeMethod();
}
I had the exact same issues. I was able to manually inject the authorization headers by using the following code:
var callcontext = new CAdxCallContext();
callcontext.codeLang = "ENG";
callcontext.poolAlias = "BGRTEST";
var proxy = new CAdxWebServiceXmlCCClient();
proxy.Endpoint.EndpointBehaviors.Add(new CustomEndpoint());
proxy.ClientCredentials.UserName.UserName = "USERNAME"; // Might not benecessary
proxy.ClientCredentials.UserName.Password = "PASSWORD"; // Might not benecessary
string inputXml = "<PARAM>" +
"<GRP ID= \"GRP1\">" +
"<FLD NAME = \"ITMREF\">" + "100001" + "</FLD>" +
"</GRP>" +
"</PARAM>";
CAdxResultXml response;
try
{
response = proxy.run(callcontext, "BGR_SIEPRO", inputXml);
}
catch (TimeoutException timeout)
{
Console.WriteLine(timeout.Message);
// handle the timeout exception.
proxy.Abort();
}
catch (CommunicationException commexception)
{
Console.WriteLine(commexception.Message);
// handle the communication exception.
proxy.Abort();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
proxy.Close();
}
}
public class ClientMessageInspector : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{
// Nothing Here
Console.Write(reply.ToString());
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " +
Convert.ToBase64String(Encoding.ASCII.GetBytes("USERNAME" + ":" +
"PASSWORD"));
request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestProperty);
return null;
}
}
public class CustomEndpoint : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
// Nothing here
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
clientRuntime.ClientMessageInspectors.Add(new ClientMessageInspector());
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
// Nothing here
}
public void Validate(ServiceEndpoint endpoint)
{
// Nothing here
}
}
Notice the Expect:100-continue in the header. That's the reason for the round trip.
Put this in your web.config and try again:
<system.net>
<settings>
<servicePointManager expect100Continue="false"/>
</settings>
</system.net>
Actually, I was wrong about this question. I did see different behaviour when running HTTP analyzer. While Http anaylzer running, my application crashed after receiving 401 response. When Http analyzer application closed, the above code worked as expected.

Type 'System.Web.HttpInputStream' cannot be serialized

I have been trying to design a WCF file upload service and am getting the following error
in my web application:
Type 'System.Web.HttpInputStream'
cannot be serialized. Consider marking
it with the DataContractAttribute
attribute, and marking all of its
members you want serialized with the
DataMemberAttribute attribute. See
the Microsoft .NET Framework
documentation for other supported
types.
Based on this error, I have tried re-marking my FileTransfer
class with DataContractAttribute and DataMemberAttribute
but that didn't do anything:
[DataContractAttribute(Namespace = "FileTransfer")]
public class FileTransfer
{
[DataMemberAttribute]
public string GetUploadStatus;
[DataMemberAttribute]
public Tuple<string, int> DoUpload;
[DataMemberAttribute]
public int UploadFile;
[DataMemberAttribute]
public FileTransferInfo FileInfo;
[DataMemberAttribute]
public Stream FileByteStream;
}
I have tried accessing my Service Trace Log with Service Trace Viewer to
see if I could get some more detail on this error. I found a number of errors
with the following message:
The message with To
'http://localhost:1242/WebProj/filetransfer.svc/mex/mex'
cannot be processed at the receiver,
due to an AddressFilter mismatch at
the EndpointDispatcher. Check that
the sender and receiver's
EndpointAddresses agree.
Which would have been useful to me but I also found the same error for:
'http://localhost:1242/WebProj/auth.svc/mex/mex'
in the same trace and I was able to authenticate just fine without any errors which made me wonder if this error is something that I should be worrying about (if any body has a suggestion as to why there is a mex/mex at my endpoint, that would be great).
So, why can't 'System.Web.HttpInputStream' be serialized? I have provided the other important aspects of my code below. Maybe somebody out there can see something that I have missed?
[DataContract(Namespace = "FileTransfer")]
public class FileTransferInfo
{
private string _guid;
private int _flag;
private long _fileSize;
private string _fileName;
private DateTime _lastUpdate;
private FileTypeEnum _fileType;
//REMOVED GETTERS AND SETTERS FOR SPACE
}
[ServiceContract(Namespace = "FileTransfer")]
public interface IFileTransferService
{
[OperationContract(Name = "DoUpload")]
Tuple<string, int> DoUpload(List<FileTransferInfo> request);
[OperationContract(Action="UploadFile", Name="UploadFile")]
int UploadFile(FileTransfer request);
}
Here is my UploadFile method that is returning the error.
int IFileTransferService.UploadFile(FileTransfer request)
{
string uploadFolder = #"C:\TempMultiFileUploads\";
int errCode = default(int);
// parameters validation omitted for clarity
try
{
string filename = request.FileInfo.FileName;
string filePath = Path.Combine(uploadFolder, filename);
using (FileStream outfile = new FileStream(filePath, FileMode.Create))
{
const int bufferSize = 65536; // 64K
Byte[] buffer = new Byte[bufferSize];
int bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outfile.Write(buffer, 0, bytesRead);
bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
}
}
}
catch (IOException e)
{
//System.IOException
errCode = 800;
}
return errCode;
}
And, below is the endpoint, binding, and bahavior of my FileTransferService:
<endpoint name="MyFileTransferEP"
address=""
binding="basicHttpBinding"
behaviorConfiguration="BasicHttpEPBehavior"
bindingConfiguration="httpLargeDataStream"
contract="FileTransfer.IFileTransferService" />
<binding name="httpLargeDataStream"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
maxBufferSize="65536"
maxReceivedMessageSize="2147483647"
messageEncoding="Mtom"
transferMode="StreamedRequest">
<behavior name="BasicHttpEPBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
</behavior>
Here is the code in my web application that calls the upload method:
FileTransferServiceClient upload = new FileTransferServiceClient();
HttpPostedFile m_objFile = default(HttpPostedFile);
FileTransfer transmit = new FileTransfer();
transmit.FileByteStream = m_objFile.InputStream;
transmit.FileInfo = new FileTransferInfo();
transmit.FileInfo.Guid = Guid.NewGuid().ToString();
transmit.FileInfo.Flag = default(int);
transmit.FileInfo.FileSize = m_objFile.ContentLength;
transmit.FileInfo.FileName = m_objFile.FileName;
transmit.FileInfo.LastUpdate = DateTime.Now;
int retParam = upload.UploadFile(transmit); // THROWS ERROR HERE
Thanks for your help.
You cannot define a data contract with a Stream member. You can either take or return a Stream as single parameter or define a message contract which has a single MessageBodyMember of type Stream, but can have multiple MessageHeaders. For more on large data streaming in WCF, please refer to this section of MSDN.