Consuming Web Service in console C# - c#

I'm trying to consume a WebService in a console application, but I keep getting this error.
'Unable to load endpoint configuration section for
'Giftcard.WebServiceSoap' contract. Found more than one configuration
for the contract. Indicate the preferred endpoint configuration
section by name.'
This is my code:
public class Program
{
static void Main(string[] args)
{
UserWebService.WebServiceSoapClient webServiceSoapClient = new UserWebService.WebServiceSoapClient();
string id = "12345543";
webServiceSoapClient.Login(id);
}
}
WEB Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WebServiceSoap">
<security mode="Transport" />
</binding>
<binding name="WebServiceSoap1" />
</basicHttpBinding>
<customBinding>
<binding name="WebServiceSoap12">
<textMessageEncoding messageVersion="Soap12" />
<httpsTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="https://localhost:444/WebService.asmx" binding="basicHttpBinding"
bindingConfiguration="WebServiceSoap" contract="UserWebService.WebServiceSoap"
name="WebServiceSoap" />
<endpoint address="https://localhost:444/WebService.asmx" binding="customBinding"
bindingConfiguration="WebServiceSoap12" contract="UserWebService.WebServiceSoap"
name="WebServiceSoap12" />
</client>
</system.serviceModel>
</configuration>
I've added the reference to the webservice from here:

Add the bindingConfiguration="WebServiceSoap" name you want to use in the constructor of UserWebService.WebServiceSoapClient("WebServiceSoap").
public class Program
{
static void Main(string[] args)
{
UserWebService.WebServiceSoapClient webServiceSoapClient = new UserWebService.WebServiceSoapClient("WebServiceSoap");
string id = "12345543";
webServiceSoapClient.Login(id);
}
}

Related

Self Hosting WCF service using app.Config files

I have self hosting WCF service that contains it's own app.Config to expose endpoints required for the service contracts. If the service is started in the programs.cs main method it all works just fine and the metadata is exposed via the browser. However, I created a HostService class based on the ServiceBase class which in the same host library and is instantiated within the program.cs file. The HostService class starts the service and has a timer method to ping other client web services for information.
My question is, when I created the HostService : ServiceBase class and instantiate it from the main(), I have to put a duplicate app.Config file in the Service Library in order for the endpoints to properly exposed and return the metadata/wsdl. I don't want to maintain 2 duplicate app.config files if possible. Currently the host library and service library both require one. Is there a way to only have just one w/ the host that could be used for both? Sorry for the dumb question, but I'm new to WCF =)
Program.cs
static void Main(string[] args){
var service = new HostService();
service.StartHostService(args);
}
HostService.cs
public partial class HostService : ServiceBase
{
internal void StartHostService(string[] args)
{
this.OnStart(args);
Console.ReadLine();
this.OnStop();
}
....
}
Short answer is no. There must be two configs, one for the client that consumes the WCF and one for the server that exposes that communication methods with the WCF.
In order for your client to work, your config must be set with Client Configuration
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<client>
<endpoint
name="endpoint1"
address="http://localhost/ServiceModelSamples/service.svc"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IHello"
behaviorConfiguration="IHello_Behavior"
contract="IHello" >
<metadata>
<wsdlImporters>
<extension
type="Microsoft.ServiceModel.Samples.WsdlDocumentationImporter, WsdlDocumentation"/>
</wsdlImporters>
</metadata>
<identity>
<servicePrincipalName value="host/localhost" />
</identity>
</endpoint>
// Add another endpoint by adding another <endpoint> element.
<endpoint
name="endpoint2">
//Configure another endpoint here.
</endpoint>
</client>
//The bindings section references by the bindingConfiguration endpoint attribute.
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IHello"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard">
<readerQuotas maxDepth="32"/>
<reliableSession ordered="true"
enabled="false" />
<security mode="Message">
//Security settings go here.
</security>
</binding>
<binding name="Another Binding"
//Configure this binding here.
</binding>
</wsHttpBinding>
</bindings>
//The behavior section references by the behaviorConfiguration endpoint attribute.
<behaviors>
<endpointBehaviors>
<behavior name=" IHello_Behavior ">
<clientVia />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
notice the <client> tag specifying how the client must call the WCF.
and with Server Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="myBindingConfiguration1" closeTimeout="00:01:00" />
<binding name="myBindingConfiguration2" closeTimeout="00:02:00" />
<binding closeTimeout="00:03:00" /> <!—- Default binding for basicHttpBinding -->
</basicHttpBinding>
</bindings>
<services>
<service name="MyNamespace.myServiceType">
<endpoint
address="myAddress" binding="basicHttpBinding"
bindingConfiguration="myBindingConfiguration1"
contract="MyContract" />
<endpoint
address="myAddress2" binding="basicHttpBinding"
bindingConfiguration="myBindingConfiguration2"
contract="MyContract" />
<endpoint
address="myAddress3" binding="basicHttpBinding"
contract="MyContract" />
</service>
</services>
</system.serviceModel>
</configuration>
Notice there is no <client> tag here.

C# WCF Error on change Endpoint

I'm doing a service to comunicate only for test and learning,
but I'm trying to change the URL Endpoint and its returning a error
n: There was no endpoint listening at http://servername:8732/TestService/ that could accept the message. This is often caused by an incorrect address or SOAP action.
That is my code:
private void button1_Click(object sender, EventArgs e)
{
var myBinding = new BasicHttpBinding();
myBinding.Security.Mode = BasicHttpSecurityMode.None;
var myEndPointAdress = new EndpointAddress("http://servername:8732/TestService/");
MyCalculatorServiceClient service = new MyCalculatorServiceClient(myBinding, myEndPointAdress);
MessageBox.Show(Convert.ToString(service.soma(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text))));
}
App.config "Edited":
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMyCalculatorService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://servername:8732/TestService/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyCalculatorService"
contract="ServiceReference1.IMyCalculatorService" name="BasicHttpBinding_IMyCalculatorService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>

WCF maxReceivedMessageSize cant set over 4215

I want to set the maxReceivedMessageSize in the App.config of the WCF Client.
If the maxReceivedMessageSize equals or is smaller then 4215 it works fine. Though when setting it to 4216 or any value above it, the default value of 65536 is taken.
My Client Code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IConexaApiServic" maxReceivedMessageSize="4216" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://svsr02.conexa.local/HelloService/ConexaApiService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IConexaApiServic"
contract="ConexaApiService.IConexaApiService" name="BasicHttpBinding_IConexaApiService" />
</client>
</system.serviceModel>
</configuration>
And The relavant Server Code
<basicHttpBinding>
<binding name="BasicHttpEndpoint_MPSAPIServic" maxReceivedMessageSize="2000000">
<security mode="TransportWithMessageCredential" />
</binding>
<binding name="BasicHttpEndpoint_HelloService" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="2000000">
</binding>
</basicHttpBinding>
<service name="IIS_test123.HelloService">
<endpoint address="ConexaApi" binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpoint_HelloService" contract="IIS_test123.IHelloService"></endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/API/ConexaApiService" />
</baseAddresses>
</host>
</service>
</services>
Any idea how to fix this?
This can be explainned. If you look at your exceptions:
System.ServiceModel.CommunicationException is an exception thrown from client side. It has the maxReceivedMessageSize from the client side. Everything is fine.
FaultException: this exception is a SOAP fault that propagate the exceptions from the service to the client application. ( http://www.codeproject.com/Articles/799258/WCF-Exception-FaultException-FaultContract). So this exception is actually coming from the service side! The maxReceivedMessageSize is the default value, and does not correspond to the maxReceivedMessageSize in your server configuration.
The address you are connecting to in your client is the service address, not configured maxReceivedMessageSize, and not the endpoint address ConexaApi which is configure with maxReceivedMessageSize="2000000". That' s why you are getting the default 65536.
And 4215 must be the size of your message if you consider that the exception does not raise if you increase it.

Embed app.config containing service info inside the application executable

My app.config is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ESDServiceSoapBinding">
<security mode="Transport" />
</binding>
<binding name="ESDServiceSoapBinding1">
<security mode="Transport" />
</binding>
<binding name="ESDServiceSoapBinding2" />
<binding name="ESDServiceSoapBinding3" />
<binding name="ESDServiceSoapBinding4">
<security mode="Transport" />
</binding>
<binding name="ESDServiceSoapBinding5" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://downloadswebregr.mydomain.com:443/ESDServiceWeb/services/ESDEntitlementServiceAPI"
binding="basicHttpBinding" bindingConfiguration="ESDServiceSoapBinding1"
contract="ESDRegService.ESDEntitlementServiceAPI" name="ESDEntitlementServiceAPI" />
<endpoint address="http://eotwasqa1.mydomain.com:19081/ESDServiceWeb/services/ESDEntitlementServiceAPI"
binding="basicHttpBinding" bindingConfiguration="ESDServiceSoapBinding3"
contract="ESDBackupService.ESDEntitlementServiceAPI" name="ESDEntitlementServiceAPI1" />
<endpoint address="https://downloadsweb.mydomain.com:443/ESDServiceWeb/services/ESDEntitlementServiceAPI"
binding="basicHttpBinding" bindingConfiguration="ESDServiceSoapBinding4"
contract="ESDBackupService.ESDEntitlementServiceAPI" name="ESDEntitlementServiceAPI2" />
</client>
</system.serviceModel>
</configuration>
I tried embedding this inside the executable using the build action "Embedded Resource" but then my application is not able to read the services information. I want to get rid of it as I want to ship only a single file. i.e. the executable.
I searched for similar questions on stackoverflow and people say, it should be outside so that it can be configured or modified from outside.
Please note that, I dont want it to get changed from outside and just want to embed it.
The basic purpose of the configuration file is to allow the external users to configure something in your application.Hence the configuration files needs to be distributed to the end users.If you intend not to allow changes to the end points ,it has to created inside your code as suggested in the previous comment.
Check the following link.
Create WCF endpoint configurations in the client app, in code?

Error "The username is not provided. Specify username in ClientCredentials." when passing CredentialCache.DefaultNetworkCredentials

I am writing a simple C# console application that will make a call to the "Lists.asmx" web service on a SharePoint site in order to pull down attachments. I've already added the service reference and tried to run it but it fails with the error
The username is not provided. Specify username in ClientCredentials.
when it tries to make a service method call to GetAttachmentCollection. Below is the code:
static void Main(string[] args)
{
ListsSoapClient client = new ListsSoapClient();
client.ClientCredentials.Windows.ClientCredential =
CredentialCache.DefaultNetworkCredentials;
XElement attachments = client.GetAttachmentCollection("Projects", "42");
// Do something with the attachments
}
As you can see I'm trying to set the ClientCredential to be the credentials for the the current user but it does not appear to be sending the user name. My SharePoint site is configured with https and Windows Authentication. Below is my simple config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpsBinding>
<binding name="ListsSoap">
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="Ntlm"
proxyCredentialType="None" realm=""/>
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpsBinding>
</bindings>
<client>
<endpoint address="https://intranet.test.com/_vti_bin/Lists.asmx"
binding="basicHttpsBinding" bindingConfiguration="ListsSoap"
contract="ListServiceProxy.ListsSoap" name="ListsSoap" />
</client>
</system.serviceModel>
</configuration>
I have tried clientCredentialType="NtlM" and clientCredentialType="Windows" but I get the same result. Is there something wrong with my configuration file or some variable that I need to set on client?

Categories

Resources