Calling a webservice from dll causing exception - c#

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.

Related

WCF - EndpointNotFoundException when calling from another Web Application (WCF or REST API)

recently I've met problem with EndpointNotFoundException when calling from another Web Application (WCF or REST API) in WCF.
I'm trying to connect to external WCF service which needs login and password identification (right now I'm working on test enviroment prepared by provider).
Then exception occours:
There was no endpoint listening at https://xxx/yyy.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details
This problem not occurs in Console Application - works smooth but only when I'm creating simple projects such as WCF or REST API.
<bindings>
<customBinding>
<binding name="custom">
<security defaultAlgorithmSuite="Default" authenticationMode="UserNameOverTransport"
requireDerivedKeys="true" includeTimestamp="true" messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10">
<localClientSettings detectReplays="false" />
<localServiceSettings detectReplays="false" />
</security>
<textMessageEncoding messageVersion="Soap11WSAddressing10" />
<httpsTransport maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647"/>
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://uslugaterytws1test.stat.gov.pl/TerytWs1.svc"
binding="customBinding" bindingConfiguration="custom" contract="TerytWsTest.ITerytWs1"
name="custom" />
</client>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 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>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
TerytWs1Client tc = new TerytWs1Client();
tc.ClientCredentials.UserName.UserName = "TestPubliczny";
tc.ClientCredentials.UserName.Password = "1234abcd";
var zalogowany = tc.CzyZalogowany();
As well I have tried:
try {
var proxy = new ChannelFactory<TerytWsTest.ITerytWs1>("custom");
proxy.Credentials.UserName.UserName = "TestPubliczny";
proxy.Credentials.UserName.Password = "1234abcd";
var result = proxy.CreateChannel();
var test = result.CzyZalogowany();
}
catch (Exception ex) { }
First of all you have to make sure that your WCF service has been started, you can check whether the WCF service has been started through the browser:
Then you can call the WCF service by adding a service reference:
After clicking OK, it will automatically generate the proxy class, we can call the WCF service through the proxy class:

WCF Serice - Getting 400 Bad Request but can use IE to browse to it

I have written a WCF Service which is being called by a Winforms application.
I have tested calling the WCF Service from the WinForms application locally and it all works fine
I have moved both the WinForms application and the WCF Service to a Remote Server, on the Remote Server I can use IE to browse to the Service but when I try to use the Winforms application I get a 400 Bad Response error.
My local machine and the Remote Server has been configured exactly the same, Windows Firewall, User accounts etc and the codebase/config files of both the Service and Winforms app are the same.
The config file for the WCF Service is as follows (i've had to remove the base address)
<bindings>
<webHttpBinding>
<binding name="webBinding">
<security mode="None">
<transport clientCredentialType="None" />
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="CodeLocksAPIServiceBehavior" name="CodeLocksAPI.WCF.CodeLocksAPIService">
<host>
<baseAddresses>
</baseAddresses>
</host>
<endpoint address="" behaviorConfiguration="webHttpBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="CodeLocksAPI.WCF.ICodeLocksAPIService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="CodeLocksAPIServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
In the Winforms app config I have tried adding the DefaultProxy tabs which do not help
WCF Service is called from Code Using the following
string serviceUserName = ConfigurationManager.AppSettings["ServiceUserName"];
string servicePassword = ConfigurationManager.AppSettings["ServicePassword"];
HttpClientHandler handler = new HttpClientHandler
{
Credentials = new
System.Net.NetworkCredential(serviceUserName, servicePassword)
};
this.Client = new HttpClient(handler);
this.Client.BaseAddress = new Uri(this.GetBaseAddressFromConfig());
processInitializeLockRequestArgs.AssetRef = assetRef;
string contentMessage = Newtonsoft.Json.JsonConvert.SerializeObject(processInitializeLockRequestArgs);
byte[] contentBytes = System.Text.Encoding.UTF8.GetBytes(contentMessage);
ByteArrayContent content = new ByteArrayContent(contentBytes);
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
httpResponse = this.Client.PostAsync(methodUri, content).Result;
As already stated - this code is working locally but not on the remote server - the WCF Service can be browsed to locally and on the remote service
I've been informed that the issue is most likely down to Proxy settings on the WinForms application and that the issue is not down to a Firewall or anything like that
You could try adding
<configuration>
<system.net>
<defaultProxy useDefaultCredentials="true"/>
</system.net>
....
to the beginning of your WinForms config file.

Having trouble connecting to a web service with a windows 8.1 phone app

This is my first time doing something like this so I'm probably going to sound stupid but here it goes.
Here is my method that is supposed to connect to the web service.
public async void getWebData()
{
var uri = new Uri("http://localhost:9011/Service.svc?singleWsdl");
var httpClient = new HttpClient();
// Always catch network exceptions for async methods
try
{
var result = await httpClient.GetStringAsync(uri);
doSomething(result);
}
catch
{
// Details in ex.Message and ex.HResult.
}
// Once your app is done using the HttpClient object call dispose to
// free up system resources (the underlying socket and memory used for the object)
httpClient.Dispose();
}
Here is my config info from the web.config file where I believe I added the rest endpoint correctly because I read that you have to use a rest endpoint to access web services in windows 8.1 phone apps.
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService" />
</basicHttpBinding>
<webHttpBinding>
<binding name="WebHttpBinding_IService" />
</webHttpBinding>
</bindings>
<services>
<service name="WcfService.Service1" behaviorConfiguration="MyServiceBehavior">
<endpoint address="http://localhost:9011/Service.svc?singleWsdl" name="rest"
binding="webHttpBinding" bindingConfiguration="WebHttpBinding_IService" contract="WcfService.IService" />
<endpoint address="http://localhost:9011/Service.svc?singleWsdl" name="soap"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService" contract="WcfService.IService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<!-- 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="jsonBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
So can anyone tell me what I'm doing wrong to access my webservice because nothing I try has been working...

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.

Bad response (400) with a WCF streaming service configured programmatically

this if my first attempt at using streaming for WCF, and I am struggling with the dreadful "The remote server returned an unexpected response: (400) Bad Request" response.
The trace viewer says that this is a System.ServiceModel.ProtocolException with message "There is a problem with the XML that was received from the network. See inner exception for more details." The inner exception type says "The body of the message cannot be read because it is empty."
Leaving everything else equal, if I switch to buffered mode on the client side, I am able to debug into the server code!
For some reason, I have to configure my service programmatically, as follows:
public IUniverseFileService OpenProxy(string serviceUrl)
{
Debug.Assert(!string.IsNullOrEmpty(serviceUrl));
var binding = new BasicHttpBinding();
binding.Name = "basicHttpStream";
binding.MaxReceivedMessageSize = 1000000;
binding.TransferMode = TransferMode.Streamed;
var channelFactory =
new ChannelFactory<localhost.IUniverseFileService>(
binding,
new EndpointAddress(serviceUrl));
return channelFactory.CreateChannel();
}
While the server is configured as follows:
<system.serviceModel>
<!-- BEHAVIORS -->
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" httpHelpPageEnabled="true"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<!-- SERVICES -->
<services>
<service behaviorConfiguration="serviceBehavior" name="Org.Acme.UniverseFileService">
<endpoint address=""
binding="basicHttpBinding"
name="basicHttpStream"
bindingConfiguration="httpLargeMessageStream"
contract="Org.Acme.RemoteCommand.Service.IUniverseFileService" />
<endpoint address="mex"
binding="mexHttpBinding"
bindingConfiguration="" name="mexStream"
contract="IMetadataExchange"/>
</service>
</services>
<!-- BINDINGS -->
<bindings>
<basicHttpBinding>
<binding name="httpLargeMessageStream"
maxReceivedMessageSize="2147483647"
maxBufferPoolSize="2147483647"
maxBufferSize="2147483647"
transferMode="Streamed"/>
</basicHttpBinding>
</bindings>
I appreciate your help!
Stefano
Everything started to work when I changed the transfer mode from Streamed to StreamedResponse as follows:
binding.TransferMode = TransferMode.StreamedResponse;
Still I don't understand why this works and Streamed does not, and why I am able to both send and receive a file stream from the server.
Streaming mode is not supported by ASP.NET development server. You need to deploy the service to IIS (or a WCF Service Application) to use Streaming mode.
Try to add messageEncoding="Mtom" in bining tag.

Categories