Dynamic WCF client doesn't wait more than one minute - c#

I write c# code, to dynamic connect to WCF server. If connect will less that 1 minutes it's work perfect. But if remote function work more than 1-2 minutes, it's throw exception. This is my code:
try
{
BasicHttpBinding basicHttpBinding = new BasicHttpBinding()
{
CloseTimeout = TimeSpan.FromMinutes(20),
OpenTimeout = TimeSpan.FromMinutes(20),
ReceiveTimeout = TimeSpan.FromMinutes(10),
SendTimeout = TimeSpan.FromMinutes(10)
};
EndpointAddress endpointAddress = new EndpointAddress("http://***");
IBrowserClicker personService = new ChannelFactory<IBrowserClicker>(basicHttpBinding, endpointAddress).CreateChannel();
Console.WriteLine(personService.TestConnect().Equals("Hello google!"));
}
catch (Exception exception)
{
Console.WriteLine("Error:"+exception);
}
Exception:
Error:System.ServiceModel.FaultException: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.
So, how it's fix?

try to turn on IncludeExceptionDetailInFaults in web.config file to see the exact error. and this is possible duplicate of
Turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server
This explain as follows, and it worked for me.
In your .config file define a behavior:
<configuration>
<system.serviceModel>
<serviceBehaviors>
<behavior name="debug">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
...
Then apply the behavior to your service along these lines:
<configuration>
<system.serviceModel>
...
<services>
<service name="MyServiceName" behaviorConfiguration="debug">
</services>
</system.serviceModel>
</configuration>
Please look at it and let me know if you find trouble to activate it.

Related

Calling a webservice from dll causing exception

I am calling a webservice to which I have created its end point in my app.config file. I have the following set in my app.config to inlcude the error stack it mentioned.
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="debug">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISmsService">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://api.collstream.co.uk/SmsService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISmsService" contract="smsService.ISmsService" name="BasicHttpBinding_ISmsService" />
</client>
</system.serviceModel>
Here is how I am calling the method as well
public void send(string from, string to, string message, string section, string userName, string password)
{
// "Services." will be whatever namespace you specified on the
// "Add Service Reference" dialog
var svc = new smsService.SmsServiceClient();
// This values will probably be coming from a config file in a real application
try
{
svc.SendMessage(from, to, message, userName, password);
}
catch (Exception ex)
{
}
finally
{
svc.Close();
}
}
This is the way that I am trying to process the webservice but i am being produced with the error below any ideas.
{"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs."}
As you can see in my app.config above I have indead set this here
IncludeExceptionDetailInFaults="true" but still i not getting the full stack trace of the error. This is a class project I am calling the service refrence from.

How to programatically add ServiceDebug behavior with endpoint?

I am working on a windows service to host WCF services for various projects. Using the following app.config fragment:
<system.serviceModel>
<services>
<service name="MyWcfService" behaviorConfiguration="MetaDataBehavior">
<endpoint contract="MyWcfService" binding="wsHttpBinding" address="" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MetaDataBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</bindings>
</system.serviceModel>
I can expose the stock service "help" page and a WSHttpBinding on the same URL from within my service like so:
ServiceHost myServiceHost = new ServiceHost(typeof(MyWcfService), new Uri(serviceAddress));
myServiceHost.Open()
I can then open a web browser and go to, e.g. http://host:8001/services/MyWcfService, and I see the standard WCF help page so I know the service is working. (For the moment, let's ignore the security implications of exposing the help page.) I can also access the WSHttpBinding endpoint on the same URL.
We have now grown to host a lot of WCF services within this service, so I'm working to simplify the configuration by adding endpoints programatically to the ServiceHost object. This all works just fine using myServiceHost.AddServiceEndpoint().
The last piece I haven't been able to get programatically is enabling the ServiceDebug behavior on the same URL as the WSHttpBinding. I have the following:
ServiceDebugBehavior sdb = myServiceHost.Description.Behaviors.Find<ServiceDebugBehavior>();
if (sdb == null)
{
sdb = new ServiceDebugBehavior()
{
HttpHelpPageEnabled = true,
IncludeExceptionDetailInFaults = true
};
myServiceHost.Description.Behaviors.Add(sdb);
}
else
{
sdb.HttpHelpPageEnabled = true;
sdb.IncludeExceptionDetailInFaults = true;
//sdb.HttpHelpPageUrl = new Uri(serviceAddress +"/help");
}
ServiceEndpoint endpoint = new ServiceEndpoint(contract, new WSHttpBinding, new EndpointAddress(new Uri(serviceAddress)));
myServiceHost.AddServiceEndpoint(endpoint);
which works, so long as I a) change the address of the help page (as commented above), or b) change the URI on which the WSHttpBinding listens. Conceptually this makes sense: .NET doesn't want to have two endpoints listening on the same URI.
My problem is that I have to maintain compatibility with existing applications using this service, which means my endpoint addresses cannot change. If I can accomplish this through the app.config, why can I not accomplish this programatically?
You can't actually ADD a ServiceDebugBehavior to a ServiceHost, what you have to do is modify the existing ServiceDebugBehavior (was having the same issue)
Example
svcHost = new ServiceHost(typeof(MyService), adrbase);
// Configure Your Service
// Now for the ServiceDebugBehavior you want to modify (Example disable HTTP Help Page)
svcHost.Description.Behaviors.Find<ServiceDebugBehavior>().HttpHelpPageEnabled = false;

Hosting NamedPipe WCF on Win7 IIS 7.5 EndpointNotFoundException

I am trying to create WCF NamedPipe in win7 with IIS 7.5.
In IIS Manager right click, and create WebSite named "TestWCFWithNamedPipe"
Right click my WebSite "TestWCFWithNamedPipe" -> select Edit bindings
Type:http
Host Name:sample.localdev.net
Port:80
Address:*
Type:net.pipe
Binding informations:*
In Advanced settings set the value for Enabled Protocol as "http,net.pipe"
In My WebSite "TestWCFWithNamedPipe" Web Application Project
web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceDebug />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DefaultBehavior" name="SampleWcfLib.SampleWcfObj">
<endpoint address="net.pipe://localhost/Sample" binding="netNamedPipeBinding"
bindingConfiguration="" contract="SampleWcfInterfaceLib.ISampleWcfObj" />
</service>
</services>
</system.serviceModel>
TestClient.exe code is following
string address = "net.pipe://localhost/Sample";
NetNamedPipeBinding binding = new NetNamedPipeBinding();
EndpointAddress ep = new EndpointAddress(address);
ISampleWcfObj channel = ChannelFactory<ISampleWcfObj>.CreateChannel(binding, ep);
string result = channel.Ping("Test");
And I run TestClient.exe, and I get an exception:
There was no endpoint listening at net.pipe://localhost/Sample that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
I have checked Win7 Service status:
Net.Pipe Listener Adapter Started
Net.Tcp Listener Adapter Started
I have done all settings for WCF.
Why do I still get the EndpointNotFoundException error message ?
You can find the name of your tcp binding if you have a look in your XSD file.
It will be on the bottom of your file and it will look like this
<wsdl:service name="PersonService">
<wsdl:port name="NetNamedPipeBinding_IPersonContract" binding="tns:NetNamedPipeBinding_IPersonContract">
<soap12:address location="net.pipe://wcftestpipe/WcfTest/PersonService.svc/test"/>
<wsa10:EndpointReference>
<wsa10:Address>net.pipe://wcftestpipe/WcfTest/PersonService.svc/test</wsa10:Address>
</wsa10:EndpointReference>
</wsdl:port>
</wsdl:service>
Afther that you propably get a security exeception that will say you don't have the right credentials. You can set your binding security mode to NONE in your web.config of your WCF service and app.config of your Client.
<bindings>
<netNamedPipeBinding>
<binding>
<security mode="None"/>
</binding>
</netNamedPipeBinding>
</bindings>
Hope this helps

Turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server

I have a WCF service that has been working perfectly, and something has changed and I don't know what.
I get this exception:
System.ServiceModel.FaultException: The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.
This is confusing because I am running .NET 4.0.
Where do I turn on IncludeExceptionDetailInFaults? I'm battling to find it.
Define a behavior in your .config file:
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="debug">
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
...
</system.serviceModel>
</configuration>
Then apply the behavior to your service along these lines:
<configuration>
<system.serviceModel>
...
<services>
<service name="MyServiceName" behaviorConfiguration="debug" />
</services>
</system.serviceModel>
</configuration>
You can also set it programmatically. See this question.
It's in the app.config file.
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDebug includeExceptionDetailInFaults="true"/>
If you want to do this by code, you can add the behavior like this:
serviceHost.Description.Behaviors.Remove(
typeof(ServiceDebugBehavior));
serviceHost.Description.Behaviors.Add(
new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true });
You can also set it in the [ServiceBehavior] tag above your class declaration that inherits the interface
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyClass:IMyService
{
...
}
Immortal Blue is correct in not disclosing the exeption details to a publicly released version, but for testing purposes this is a handy tool. Always turn back off when releasing.
I was also getting the same error, the WCF was working properly for me when i was using it in the Dev Environment with my credentials, but when someone else was using it in TEST, it was throwing the same error.
I did a lot of research, and then instead of doing config updates, handled an exception in the WCF method with the help of fault exception.
Also the identity for the WCF needs to be set with the same credentials which are having access in the database, someone might have changed your authority.
Please find below the code for the same:
[ServiceContract]
public interface IService1
{
[OperationContract]
[FaultContract(typeof(ServiceData))]
ForDataset GetCCDBdata();
[OperationContract]
[FaultContract(typeof(ServiceData))]
string GetCCDBdataasXMLstring();
//[OperationContract]
//string GetData(int value);
//[OperationContract]
//CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
[DataContract]
public class ServiceData
{
[DataMember]
public bool Result { get; set; }
[DataMember]
public string ErrorMessage { get; set; }
[DataMember]
public string ErrorDetails { get; set; }
}
in your service1.svc.cs you can use this in the catch block:
catch (Exception ex)
{
myServiceData.Result = false;
myServiceData.ErrorMessage = "unforeseen error occured. Please try later.";
myServiceData.ErrorDetails = ex.ToString();
throw new FaultException<ServiceData>(myServiceData, ex.ToString());
}
And use this in the Client application like below code:
ConsoleApplicationWCFClient.CCDB_HIG_service.ForDataset ds = obj.GetCCDBdata();
string str = obj.GetCCDBdataasXMLstring();
}
catch (FaultException<ConsoleApplicationWCFClient.CCDB_HIG_service.ServiceData> Fex)
{
Console.WriteLine("ErrorMessage::" + Fex.Detail.ErrorMessage + Environment.NewLine);
Console.WriteLine("ErrorDetails::" + Environment.NewLine + Fex.Detail.ErrorDetails);
Console.ReadLine();
}
Just try this, it will help for sure to get the exact issue.
As the error information said first please try to increase the timeout value in the both the client side and service side as following:
<basicHttpBinding>
<binding name="basicHttpBinding_ACRMS" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
openTimeout="00:20:00"
receiveTimeout="00:20:00" closeTimeout="00:20:00"
sendTimeout="00:20:00">
<readerQuotas maxDepth="32" maxStringContentLength="2097152"
maxArrayLength="2097152" maxBytesPerRead="4006" maxNameTableCharCount="16384" />
</binding>
Then please do not forget to apply this binding configuration to the endpoint by doing the following:
<endpoint address="" binding="basicHttpBinding"
bindingConfiguration="basicHttpBinding_ACRMS"
contract="MonitorRAM.IService1" />
If the above can not help, it will be better if you can try to upload your main project here, then I want to have a test in my side.

WCF Reading Endpoint Behaviors from a web.config file

I've have a WCF service which has multiple clients that it connects to.
What I want to do is to create the clients dynamically the WCF services consumes.
Creating the clients by inheriting from the ServiceFactory<TChannel> class is done and very simple. What I'm struggling with is how to read Endpoint behaviours from the web.config file and add them to the clients?
Code file
BasicHttpBinding binding = new BasicHttpBinding(bindingConfigName);
EndpointAddress endpoint = new EndpointAddress(endpointUrl);
ChannelFactory<IShoppingSoap> clientEndpoint = new ChannelFactory<IShoppingSoap>(binding, endpoint);
base.Endpoint.Behaviors.Add(*Get the behavior from the config file*);
return base.CreateChannel();
Web.config file :
<behaviors>
<endpointBehaviors>
<behavior name="EndpointBehaviour_GmCustom">
<dataContractSerializer maxItemsInObjectGraph="2147483646" />
<behavior>
</endpointBehaviors>
</behaviors>
Found the solution.. i think.. you have to go through each of the operations in the endpoint and change the maxItemsInObjectGraph there.
foreach (OperationDescription operation in base.Endpoint.Contract.Operations)
{
operation.Behaviors.Find<DataContractSerializerOperationBehavior>().MaxItemsInObjectGraph = 2147483646;
}
Found the solution here
http://www.lapathy.com/home/2009/9/30/programmatically-setting-maxitemsinobjectgraph-in-wcf.html

Categories