class library application not using the app.config file - c#

I am facing problem while consuming the webservice and making an web service call.
I am creating the class library application which will generate the dll and this dll will be used by some other application to connect the webservice.I have received the WSDL file from third part and I "Add a service reference" and used all the proxy classes to create my soap body. Now I have two issues I need to add the wsse:security header to my soap message like below
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password>password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
To handle this and to connect the webserivce endpoint address have modified my app.config file (which got generated when add service reference) to include this as header tag. below is my app.config file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ICMS-Http-WSBinding">
<security mode="Transport">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://devs-dp-in.wellpoint.com/Claims/2.0/Get_iHealthICMS_Results"
binding="basicHttpBinding" bindingConfiguration="ICMS-Http-WSBinding"
contract="Ihlth_Service1.ICMSHttpWSPortType" name="ICMS-Http-WSPortType">
<headers>
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
<wsse:Username>username</wsse:Username>
<wsse:Password>password</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</headers>
</endpoint >
</client>
</system.serviceModel>
</configuration>
When I tried to initialize my client as below I am facing the error as Could not find endpoint element with name 'ICMS-Http-WSPortType' and contract 'Ihlth_Service.ICMSHttpWSPortType' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element
Service.CMSHttpWSPortTypeClient Client = new Service.CMSHttpWSPortTypeClient("ICMS-Http-WSPortType");
I modified my code to create a binding and endpoint address inside the code and pass to the client then it worked fine but again faced issue at the security header missing as I provided the security information in app.config file
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
EndpointAddress address = new EndpointAddress("https://devs-dp-in.wellpoint.com/Claims/2.0/Get_iHealthICMS_Results");
is there any way that I can read the app.config file or my code refer that config file to get the information rather giving inside the code.
This entire code is working fine in the windows application by reading the app.config file but not in the class library application is there any reason behind that ?
Any one please give some idea

The app.config of a DLL is, at best, a reminder of what needs to be placed in the app.config of the EXE that consumes your DLL or the web.config of a website that consumes your DLL.
It's not actually used by default by the configuration system, and it's slightly unfortunate that some tooling (such as Add Service Reference) will create one in a class library project and give no warning about this.

Related

SoapClient in C# dll called in C++ project : no endpoint found

I have a webservice, created by a Java application. I want to call its services from a C++ project. I've been trying gsoap and other C++ code generators, but they're all out of date or unsupported.
So I decided to add an interface in C#, meaning that I will create a SOAP client in C# that will call each function of the webservice (in VS2015, you can only do that in Windows Form Application, dunno why ...).
Then I will export these functions by compiling the C# project as a DLL with the nuget UnmanagedExports of robert giesecke, dll which I will load in my end project in C++.
However, when I try to call the webservice in the C++ script, my app crashes with this log
Unhandled Execption: System.InvalidOperationException :
Could not find default endpoint element that references contract DockersWS.DockersWS
in the ServiceModel client configuration section. This might be because no configuration
file was found in your application or because no endpoint element matching this contract
could be found in the client element
at RawCSSoap\Services references\DockersWS\Reference.cs line 898
DockersWS is the name of the webservice called in the C# SOAP Client. The line 898 from References.cs looks like this :
public DockersWSClient() {}
I create the client in the C# app like this, directly in the function I'm exporting.
DockersWS.DockersWSClient client = new DockersWS.DockersWSClient();
My app.config generated by VS2015 when I added the Service Reference looks like this:
<?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="DockersWSPortBinding" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://nxl35726:8080/DockersWS/DockersWS"
binding="basicHttpBinding" bindingConfiguration="DockersWSPortBinding"
contract="DockersWS.DockersWS" name="DockersWSPort" />
</client>
</system.serviceModel>
And the part of the WSDL file dealing with endpoint looks like this:
<binding name="DockersWSPortBinding" type="tns:DockersWS">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
<!--bunch of operations-->
</binding>
<service name="DockersWS">
<port name="DockersWSPort" binding="tns:DockersWSPortBinding">
<soap:address location="http://nxl35726:8080/DockersWS/DockersWS" />
</port>
</service>
I've seen that sometimes a web.config file is generated also. But I didn't get it. Maybe that's the issue, but how can I generated it, then ?
I tried to add the app.config file to the C++ project as a Resource, but it didn't help.
So my question is : how can I fully use the SOAP client functions in the C++ project, without having this endpoint issue?
FOUND THE TRICK:
All this mess comes from the fact the app.config in the C# project is not exported in the DLL. So the C++ project has no way to configure its call to create a Client.
Therefore, the solution is to hard-code the client configuration in a C# function, export this function and call it in your C++ application.
Here is the code to replace the app.config file
BasicHttpBinding basicHttpbinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
basicHttpbinding.Name = "DockersWSPortBinding";
basicHttpbinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
basicHttpbinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
EndpointAddress endpointAddress = new EndpointAddress("http://nxl35726:8080/DockersWS/DockersWS?wsdl");
proxyClient = new DockersWS.DockersWSClient(basicHttpbinding, endpointAddress);
}

How To Hard Code App.Config In dll file? C#

I am writing dll file for SAP B1, i need to embed App.Config in dll, no other option. Dll is using Web Service.
This is how my App.config looks like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WayBillsSoap">
<security mode="Transport" />
</binding>
<binding name="WayBillsSoap1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://services.rs.ge/WayBillService/WayBillService.asmx"
binding="basicHttpBinding" bindingConfiguration="WayBillsSoap"
contract="WayBillWS.WayBillsSoap" name="WayBillsSoap" />
</client>
</system.serviceModel>
</configuration>
You can't hard-code the App.config itself, because it simply isn't designed that way.
You can create your own config and embed it into your application using, for example, a resource file (.resx) or using constants to embed strings in a custom class.
But there is no way to embed the dll.
.NET does allow assemblies such as dlls to have their own yourdll.dll.config file. But you can't embed it. It must sit alongside you dll, just as it does for executables.
======= Edit after you posted your config =======
Ah. So here's the question. If you embed your config, that means by definition it can't change. So since you're okay with your config not changing -- since you want to embed it -- and it looks like you're using WCF, I would suggest you look at programmatically creating your WCF endpoint.
In WCF you can configure your endpoint in code instead of using an App.Config. That way you don't need a config at all.
Unfortunately, teaching your how to do this is beyond the scope of this question, but take a look at this answer: Programatically adding an endpoint. And try this google search: "wcf endpoint programmatically". That should help show you how to programmatically create a WCF endpoint.
I'm unsure about completely replacing the App.config but you can add and modify things found in it from code using the System.Configuration I've specifically used the System.Configuration.ConfigurationManager.AppSettings["SomeKey"]
Edit:
Doing that doesn't rewrite the App.config it simply temporarily adds/changes it

Error connecting to asmx webservice on silverlight

i'm trying to connect a silverlight 4 application with an asmx webservice, but i'm getting an "InvalidOperationException" when trying to do so.
The problem occurs on the first line of this function
private void AskWebService() {
WSCalledSoapClient client = new WSCalledSoapClient();
client.AskThisCompleted += new EventHandler<AskThisCompletedEventArgs>(client_AskThisCompleted);
client.AskThisAsync("the answer to life, universe and everything");
}
The error of the exception should translate something like this (haven't found the english version of it)
element 'message' not recognized on the service reference configuration. Note that only a subset of the Windows Communication Foundation configuration functionality is available in Silverlight.
Details of the webservice:
Is published outside the silverlight project
Files clientaccesspolicy.xml and crossdomain.xml are in place
Details of the Silverlight App
SL version is 4.0, .net framework is also 4.0
Given the same config and same treatment (i.e. steps to import the webservice, code used to call the webservice), the call to the webservice works without problem on a new silverlight application that contains only a button and a textblock
The app uses others webservices, but only one is giving problem.
Update:
Here is the ServiceReferences.ClientConfig
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WSCalledSoap" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://myip:someport/WSCalled.asmx"
binding="basicHttpBinding" bindingConfiguration="WSCalledSoap"
contract="SrvWSCalled.WSCalledSoap" name="WSCalledSoap" />
</client>
</system.serviceModel>
</configuration>
Any help will be appreciated.
Thanks.

Creating and Consuming a Webservice entirely within a DLL

Hope someone here can help me solve this !
I'm writing a custom plugin for Microsoft CRM, which on the creation or update of certain entities, carries out some tasks in the background on our Sharepoint instance, which can't be completed via CRM workflows as they don't have the functionality.
The problem is, that when creating the Service Reference, the defintion for the webservice, with the endpoint address, etc is stored in the DLL's .config file. When deploying the CRM Plugin, the .config file isn't available either when deploying it to disk, or to the database, and the plugin fails as soon as the I try to use the Webservice.
I've had a look at some documentation for BasicHttpBinding, but I'm not entirely sure how I would go about creating the Webservice programmatically, so that it is entirely contained within the DLL.
I'm not bothered about being able to amend the details without recompiling, as the code will be pretty much static anyway.
I've tried defining the Service Reference in the project, as "TestDws"
BasicHttpBinding SharepointWS = new BasicHttpBinding();
SharepointWS.Name = "SharepointWebservice";
EndpointAddress EndPoint = new EndpointAddress("http://hostname/_vti_bin/dws.asmx");
TestDws.DwsSoapClient temp = new TestDws.DwsSoapClient(SharepointWS, EndPoint);
However, the plugin just craps out at this point.
The original .config doesnt have much more in the way of configuration
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="DwsSoap" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://hostname/_vti_bin/dws.asmx"
binding="basicHttpBinding" bindingConfiguration="DwsSoap"
contract="TempDws.DwsSoap" name="DwsSoap" />
</client>
</system.serviceModel>
</configuration>
I recreated the Service Reference from scratch, and used the same BasicHttpBinding method as above and it worked second time round... albeit now with a HTTP401 on the Sharepoint Webservice !

Placeholder URL for SharePoint web services

tl;dr
Below is the app.config from my class library which simplifies file manipulation in SharePoint. The endpoint addresses point at an arbitrary site but are set dynamically to the correct site when the library is used.
Will the DLL still work if the arbitrary address changes/ceases to exist?
Can I package my class library to dynamically add endpoint's and binding's to the project that references the library (Based on the site they interact with)?
Details
I created a class library in C# to simplify access to SharePoint sites via the Copy and Lists web services. By passing in the site URL and other data, the end user can easily upload, download, and manipulate files in SharePoint. When they pass the site URL, I dynamically set the Copy or Lists reference to use that URL. While this works out fine, I have two questions:
I must include a reference in the app.config/web.config to one SharePoint server to get the web service details when developing. If that URL changes or ceases to exist, then will my library fail?
Even though the DLL is included when referenced, the app.config containing the basicHttpBinding and endpoint addresses is not included. Is there a way to add that data in the web.config/app.config of the project that is using the class library?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
</appSettings>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="CopySoap" maxReceivedMessageSize="419430400">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
<binding name="ListsSoap">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Ntlm" proxyCredentialType="None" realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://www.example.com/subsite/_vti_bin/copy.asmx"
binding="basicHttpBinding" bindingConfiguration="CopySoap"
contract="CopyService.CopySoap" name="CopySoap" />
<endpoint address="http://www.example.com/subsite/_vti_bin/Lists.asmx"
binding="basicHttpBinding" bindingConfiguration="ListsSoap"
contract="ListsService.ListsSoap" name="ListsSoap" />
</client>
</system.serviceModel>
</configuration>
Thanks for your time.
My Answers
If you are writing it in App.Config, there is no way it's hard coded. It's a config file. You can change it whenever you want and you do not need to rebuild your project. I hope for you are checking wheather the Site/Server exists before proceeding with functionality. If the server/site does not exist - handle it gracefully and show appropiate message so that a user can go and change the config file to point it to correct server
You can simply add the configs required for your service in the "Referenced Project"'s web or app.config. Since your DLL in running in the context of the Referenced Project, it would pcik the necessary values from its app/web.config

Categories

Resources