Embed app.config containing service info inside the application executable - c#

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?

Related

Consuming Web Service in console 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);
}
}

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.

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.

No endpoint listening at local "..."

I have been given a wcf service, and i built a local console applicattion to test it, but i keep getting this error shown in the title. My service runs in the browser as it should, showing the screen where it shows the example and the url where you can test it. Probably the error is in the Web.config or in the App.config. I have this two files:
Web.condig
<?xml version="1.0"?>
<configuration>
<appSettings>
...
</appSettings>
<system.web>
<compilation debug="false" targetFramework="4.0"/>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"/>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service name="RAHPEDWCFService.RAHPEDService">
<endpoint address="http://localhost:44184/RAHPEDService.svc" behaviorConfiguration="webHttpBehavior" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="RAHPEDWCFService.IRAHPEDService"/>
</service>
</services>
</system.serviceModel>
</configuration>
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IRAHPEDService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:44184/RAHPEDService.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IRAHPEDService"
contract="ServiceReference.IRAHPEDService"
name="BasicHttpBinding_IRAHPEDService" />
</client>
</system.serviceModel>
</configuration>
I hope someone could give me a hint.
Please follow the below steps to resolve it
Please browse your service and click on the Url whether it is showing the metadata or not.
ex:http: //oj:23/Myservice?wsdl
If metadata is not enabled then do the below changes to your service config file.
i. create a mex endpoint
ii. Name the service behavior
Ex:
< serviceBehaviors>
< behavior name="SerBehavior">
< serviceMetadata httpGetEnabled="true"/>
< /behavior>
< /serviceBehaviors>
iii. Add this behavior to your service
Ex:
< service name="RAHPEDWCFService.RAHPEDService" behaviorConfiguration="SerBehavior"> < /service>
iv. Build the service and browse click on url to check the metaData
In your console application right click on the project and click on Add Service Reference
Place the Url in the Address box, click on Go check whether you are able to see the service, select your service and give namespace and click OK
create the object of your ServiceClient
Call your method
able to see the wsdl metadata in the browser.
If you have multiple endpoints in your client configuration file then you have pass the name of the endpoint to the constructor of ServiceCleint Class
Open Visual studio CommandPrompt and type WcfTestClient, right click on MyServiceProjects, add the service and check whether you are able to add or not and once added call your method under the endpoint appears
You should check that you are accessing the correct address. Rather than specify the entire address in the Web.config, more common practice is to provide just a relative address (even
an empty "" is fine), and then you access the service with:
http://servername:[port]/[virtual directory]/RAHPEDWCF.svc/[relative address in Web.config]
E.g. if address in Web.config showed
endpoint address="MySVC"
the full URL for clients (assuming default port 80 and in root of virtual server) might be
http://servername/RAHPEDWCF.svc/MySVC
If you are able to modify Web.config, this is what I would advise.

Two webprojects in the same Azure webrole?

I am trying to run multiple websites at the same web role. How it can be done?
Creating a web role that contains multiple web sites is pretty easy. Essentially, you need to add multiple elements to your web role’s ServiceDefinition.csdef file. Each element would include a physicalDirectory element that references the location of the web site to be included.
<Sites>
<Site name="WebRole1" physicalDirectory="..\..\..\WebRole1">
<Bindings>
<Binding name="Endpoint1" endpointName="Endpoint1" />
</Bindings>
</Site>
<Site name="WebApplication1" physicalDirectory="..\..\..\WebApplication1\">
<Bindings>
<Binding name="Endpoint1" endpointName="Endpoint2" />
</Bindings>
</Site>
<Site name="WebApplication2" physicalDirectory="..\..\..\WebApplication2\">
<Bindings>
<Binding name="Endpoint1" endpointName="Endpoint3" />
</Bindings>
</Site>
</Sites>
Referance:
http://michaelcollier.wordpress.com/2013/01/14/multiple-sites-in-a-web-role/
and
http://msdn.microsoft.com/en-us/library/windowsazure/gg433110.aspx
Step By Step Approch Link:
http://blog.elastacloud.com/2011/01/11/azure-running-multiple-web-sites-in-a-single-webrole/
Hope its helpful.

Categories

Resources