File Transferring through WCF - c#

Consider the following contracts
[OperationContract]
[FaultContract(typeof(CcmFaultException))]
void UploadStream(DataFileStream input);`
[MessageContract]
public class DataFileStream
{
[MessageHeader(MustUnderstand = true)]
public String FileName { get; set; }
[MessageHeader(MustUnderstand = true)]
public long FileSize { get; set; }
[MessageBodyMember(Order = 1)]
public Stream StreamData { get; set; }
}
I do have two major problems with it.
First issue is in service side which I cannot change the UploadStream signature, when I change the method signature in both interface and related class (for example adding return type or more input parameters), I can build the service but when I run the host application I get the following error:
[System.InvalidOperationException]
The operation UploadStream either has a parameter or a return type that is attributed with MessageContractAttribute.
In order to represent the request message using a Message Contract, the operation must have a single parameter attributed with ...
Another issue is in client side, even if I use the UploadStream(DataFileStream data) contract
I can create an object of DataFileStream class, but when I want to call the UploadStream method I cannot pass the object! And the only option I see is the following signature in client side:
UploadStream(string FileName, long FileSize, Stream StreamData);
Would you please give me a solution for both issues.
Thank you
P.S : I'm using the following configuration:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="CCMService.Binding_ICcmWcfService" closeTimeout="00:10:00"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
maxReceivedMessageSize="2147483647">
<reliableSession ordered ="false" inactivityTimeout ="00:05:00"
enabled ="true" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
negotiateServiceCredential="true" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://localhost:8731/CCMService"
binding="wsHttpBinding"
bindingConfiguration="CCMService.Binding_ICcmWcfService"
contract="CCMServiceRef.ICcmWcfService"
name="CCMService.Binding_ICcmWcfService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>

#Steve16351
Consider the following implementation in Client app
private void btnImport_Click(object sender, EventArgs e)
{
CCMServiceRef.DataFileStream obj = new CCMServiceRef.DataFileStream()
{
FileName = _FileName,
StreamData = GetStream(_FilePath)
};
Program.serviceManager.Client.ImportScript(obj);
}
private Stream GetStream(string filePath)
{
System.IO.MemoryStream data = new System.IO.MemoryStream();
System.IO.Stream str = File.OpenRead(filePath);
str.CopyTo(data);
data.Seek(0, SeekOrigin.Begin);
byte[] buf = new byte[data.Length];
data.Read(buf, 0, buf.Length);
return data;
}
when I switch over to DataContract program compile and host app runs, but when I call the Service method ( Program.serviceManager.Client.ImportScript(obj) ) , program sits for minutes until inactivity timeout raises!
In fact, Service method never executed!

Related

InnerChannel = 'webServiceProxy.InnerChannel' threw an exception

I am trying to Consume WCF Service for SSRS Reports.
I have tried to exceed the message size limit to But it gives me with the exceptions which is as follows :
InnerChannel = 'webServiceProxy.InnerChannel' threw an exception of type 'System.ServiceModel.CommunicationObjectFaultedException'
Channel = 'webServiceProxy.Channel' threw an exception of type 'System.ServiceModel.CommunicationObjectFaultedException'
Code to Export on Server Side to Consume WCF..
try
{
var webServiceProxy = new ReportExecutionServiceSoapClient(wcfEndpointConfigName);
// End Point Name is Coming here which is written in web Config file : basicHttpEndpoint
webServiceProxy.ClientCredentials.Windows.AllowedImpersonationLevel =
System.Security.Principal.TokenImpersonationLevel.Impersonation;
webServiceProxy.ClientCredentials.Windows.ClientCredential = clientCredentials;
// Init Report to execute
ServerInfoHeader serverInfoHeader;
ExecutionInfo executionInfo;
ExecutionHeader executionHeader = webServiceProxy.LoadReport(null, report, null,
out serverInfoHeader, out executionInfo);
// Attach Report Parameters
webServiceProxy.SetExecutionParameters(executionHeader, null, parameters, null, out executionInfo);
// Render
serverInfoHeader =
webServiceProxy.Render(executionHeader, null, GetExportFormatString(format), null,
out output, out extension, out mimeType, out encoding, out warnings,
out streamIds);
}
catch (FaultException e)
{
throw new FaultException(e.Message);
}
Web Config File
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ReportExecutionServiceSoap" receiveTimeout="05:00:00"
sendTimeout="05:00:00" allowCookies="true" maxReceivedMessageSize="5242880">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://../ReportServer/ReportExecution2005.asmx"
binding="basicHttpBinding" bindingConfiguration="ReportExecutionServiceSoap"
contract="MyServiceReference.ReportExecutionServiceSoap" name="basicHttpEndpoint" />
</client>
</system.serviceModel>
Can some onw guide me where I am doing wrong I been stuck into this for days.

Add node system.serviceModel in APP.Config C# in runtime

I have the need to add to my app.config file this section while my program is running
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="InvioTelematicoSS730pMtomPortBinding" messageEncoding="Mtom">
<security mode="Transport" />
</binding>
<binding name="RicevutaPdf730PortBinding">
<security mode="Transport" />
</binding>
<binding name="RicevutaPdf730PortBinding1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:9080/InvioTelematicoSS730pMtomWeb/InvioTelematicoSS730pMtomPort"
binding="basicHttpBinding" bindingConfiguration="InvioTelematicoSS730pMtomPortBinding"
contract="InvioFlussi730.InvioTelematicoSS730pMtom" name="InvioTelematicoSS730pMtomPort" />
<endpoint address="https://invioSS730pTest.sanita.finanze.it/Ricevute730ServiceWeb/ricevutePdf"
binding="basicHttpBinding" bindingConfiguration="RicevutaPdf730PortBinding"
contract="ServiceReference1.RicevutaPdf730" name="RicevutaPdf730Port" />
</client>
is there any way possible? Thanks in advance
.NET exposes configuration element classes for WCF services to manage them at runtime. Wrote a simple method to construct and generate the sections for you.
private static void WriteWCFConfig()
{
//standard method from System.Configuration
Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//the main section in the app.config file for WCF
ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);
var httpBindings = serviceModel.Bindings.BasicHttpBinding;
if (!httpBindings.ContainsKey("InvioTelematicoSS730pMtomPortBinding"))
{
BasicHttpBindingElement newHttpBindng = new BasicHttpBindingElement("InvioTelematicoSS730pMtomPortBinding");
newHttpBindng.MessageEncoding = WSMessageEncoding.Mtom;
newHttpBindng.Security.Mode = BasicHttpSecurityMode.Transport;
httpBindings.Bindings.Add(newHttpBindng);
}
if (!httpBindings.ContainsKey("RicevutaPdf730PortBinding"))
{
BasicHttpBindingElement newHttpBindng = new BasicHttpBindingElement("RicevutaPdf730PortBinding");
newHttpBindng.MessageEncoding = WSMessageEncoding.Mtom;
newHttpBindng.Security.Mode = BasicHttpSecurityMode.Transport;
httpBindings.Bindings.Add(newHttpBindng);
}
//the section
ChannelEndpointElementCollection endPoints = serviceModel.Client.Endpoints;
//Get endpoint names
List<string> endpointNames = new List<string>();
foreach (ChannelEndpointElement endpointElement in endPoints)
{
endpointNames.Add(endpointElement.Name);
}
if (!endpointNames.Contains("InvioTelematicoSS730pMtomPort"))
{
ChannelEndpointElement endPoint = new ChannelEndpointElement(new EndpointAddress("http://localhost:9080/InvioTelematicoSS730pMtomWeb/InvioTelematicoSS730pMtomPort"), "InvioFlussi730.InvioTelematicoSS730pMtom");
endPoint.Name = "InvioTelematicoSS730pMtomPort";
endPoint.Binding = "basicHttpBinding";
endPoint.BindingConfiguration = "InvioTelematicoSS730pMtomPortBinding";
endPoints.Add(endPoint);
}
if (!endpointNames.Contains("RicevutaPdf730Port"))
{
ChannelEndpointElement endPoint = new ChannelEndpointElement(new EndpointAddress("https://invioSS730pTest.sanita.finanze.it/Ricevute730ServiceWeb/ricevutePdf"), "ServiceReference1.RicevutaPdf730");
endPoint.Name = "RicevutaPdf730Port";
endPoint.Binding = "basicHttpBinding";
endPoint.BindingConfiguration = "RicevutaPdf730PortBinding";
endPoints.Add(endPoint);
}
appConfig.Save();
}
You can take it and modify it according to your needs.
Hope this helps.

How to invoke WCF RESTful Service in Windows Service in asp.net?

I have created below Operation Contract for POST Method in WCF RESTfule Service.
IService1.cs:-
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "/SaveCustomerPost",
BodyStyle = WebMessageBodyStyle.Wrapped)]
string SaveCustomerDetails(CustomerDetails objCustomerDetails);
[DataContract]
public class CustomerDetails
{
[DataMember]
public string Name { get; set; }
}
Windows Service:-
using (WebChannelFactory<ServiceReference1.IService1> cf = new WebChannelFactory<ServiceReference1.IService1>(new Uri("http://xxx/CustomerService.svc/SaveCustomerPost")))
{
var helloService = cf.CreateChannel();
ServiceReference1.CustomerDetails objCustomerDetails = new ServiceReference1.PANNoDetails();
objPANNoDetails.Name = "TestName";
string strResult = helloService.SaveCustomerDetails(objPANNoDetails);
}
Client App.config:-
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="CustBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="WebHttpBinding" sendTimeout="00:05:00" maxBufferSize="2147483647"
maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
transferMode="Streamed">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<client>
<endpoint address=""
binding="webHttpBinding" behaviorConfiguration="CustBehavior"
contract="ServiceReference1.ICustService" name="WebHttpBinding" />
</client>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0"/>
</system.serviceModel>
There was no endpoint listening at "xxx.svc/SaveCustomerDetails" that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
When I invoke above mentioned method with WebInvoke method in window service I got above mentioned error. When I invoke above mentioned service without WebInvoke method, service is working fine. How to resolve above mentioned issue?
When creating the ChannelFactory, try to specify the service name only:
using (ChannelFactory<ServiceReference1.ICustService> factory = new ChannelFactory<ServiceReference1.ICustService>(
new WebHttpBinding(),
"http://xxx/CustomerService.svc"))
{
}

Error while sending file to WCF: The request failed with HTTP status 400: Bad Request

I am trying to upload file using wcf service. I am getting The request failed with HTTP status 400: Bad Request at:
ds = WcfSpend.RegisterSupplier("blah", new SpendWcfRef.FileData
{
FileName = myFile.FileName.ToString(),
BufferData = file,
FilePosition = 1
});
WCF:
public DataSet RegisterSupplier(string SupplierId, FileData file)
{
DataSet ds = new DataSet();
return ds;
}
[ServiceContract]
public interface ISPEND
{
[OperationContract]
DataSet executeProcedure(string procedurename, string[] paramsName, string[] paramsValue, int num);
[OperationContract]
DataSet RegisterSupplier(string SupplierId, FileData file);
//[OperationContract]
//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; }
}
APPLICATION:
ds = WcfSpend.RegisterSupplier("blah", new SpendWcfRef.FileData
{
FileName = myFile.FileName.ToString(),
BufferData = file, FilePosition = 1
});
Apllication config file:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISPEND"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:10:00"
sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
messageEncoding="Text"
textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client/>
</system.serviceModel>
WCF WEB CONFIG:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="myBindingForBigArrays"
openTimeout="00:10:00"
closeTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
maxReceivedMessageSize="2147483647"
maxBufferPoolSize="2147483647"
maxBufferSize="2147483647">
<readerQuotas maxDepth="64"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="4096"
maxNameTableCharCount="16384"/>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="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" />
</system.serviceModel>
There could be a few things that are resulting in the error you are seeing. The first thing I would recommend is that you assign the binding configurations you created to an explicitly defined endpoint. Something like this:
<!-- This goes in the <system.serviceModel> section -->
<services>
<service name="MyService">
<endpoint address=""
binding="wsHttpBinding"
bindingConfiguration="myBindingForBigArrays"
contract="<namespace>.ISpend" />
</service>
</services>
The above is the configuration for the service. Make sure you fully qualify the contract name with the namespace, and the service name needs to be the same as the name in the markup of the .svc file.
You would do something similar for the client, except it would be the <client> tag, rather than <service>.
If you don't specify the binding configuration to use in the endpoint, then WCF will use the default (lower) values for the binding type you selected. An alternative is to make the binding configuration the default for that kind of binding, by omitting the name attribute.
If this doesn't solve the issue, you can also try adjusting the maxRequestLength value for the <httpRuntime> element in <system.web>. <system.web> is a child of <configuration>:
<system.web>
<httpRuntime maxRequestLength="2147483647" />
</system.web>

How to consume a WCF with changing endpoint or server address [duplicate]

I have my first WCF example working. I have the host on a website which have many bindings. Because of this, I have added this to my web.config.
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
This is my default binding http://id.web, which works with the following code.
EchoServiceClient client = new EchoServiceClient();
litResponse.Text = client.SendEcho("Hello World");
client.Close();
I am now trying to set the endpoint address at runtime. Even though it is the same address of the above code.
EchoServiceClient client = new EchoServiceClient();
client.Endpoint.Address = new EndpointAddress("http://id.web/Services/EchoService.svc");
litResponse.Text = client.SendEcho("Hello World");
client.Close();
The error I get is:
The request for security token could not be satisfied because authentication failed.
Please suggest how I may change the endpoint address at runtime?
Additional here is my client config, requested by Ladislav Mrnka
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IEchoService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
allowCookies="false">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://id.web/Services/EchoService.svc" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IEchoService" contract="IEchoService"
name="WSHttpBinding_IEchoService">
<identity>
<servicePrincipalName value="host/mikev-ws" />
</identity>
</endpoint>
</client>
</system.serviceModel>
So your endpoint address defined in your first example is incomplete. You must also define endpoint identity as shown in client configuration. In code you can try this:
EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
var address = new EndpointAddress("http://id.web/Services/EchoService.svc", spn);
var client = new EchoServiceClient(address);
litResponse.Text = client.SendEcho("Hello World");
client.Close();
Actual working final version by valamas
EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
Uri uri = new Uri("http://id.web/Services/EchoService.svc");
var address = new EndpointAddress(uri, spn);
var client = new EchoServiceClient("WSHttpBinding_IEchoService", address);
client.SendEcho("Hello World");
client.Close();
This is a simple example of what I used for a recent test.
You need to make sure that your security settings are the same on the server and client.
var myBinding = new BasicHttpBinding();
myBinding.Security.Mode = BasicHttpSecurityMode.None;
var myEndpointAddress = new EndpointAddress("http://servername:8732/TestService/");
client = new ClientTest(myBinding, myEndpointAddress);
client.someCall();
app.config
<client>
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="LisansSoap"
contract="Lisans.LisansSoap"
name="LisansSoap" />
</client>
program
Lisans.LisansSoapClient test = new LisansSoapClient("LisansSoap",
"http://webservis.uzmanevi.com/Lisans/Lisans.asmx");
MessageBox.Show(test.LisansKontrol("","",""));
We store our URLs in a database and load them at runtime.
public class ServiceClientFactory<TChannel> : ClientBase<TChannel> where TChannel : class
{
public TChannel Create(string url)
{
this.Endpoint.Address = new EndpointAddress(new Uri(url));
return this.Channel;
}
}
Implementation
var client = new ServiceClientFactory<yourServiceChannelInterface>().Create(newUrl);

Categories

Resources