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
Related
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);
}
I have a Windows Forms application that connects to a web service that is configured in the app.config under the typical configuration\system.serviceModel\client\endpoint node like so:
<system.serviceModel>
<bindings>
...
</bindings>
<client>
<endpoint address="http://blahblah/service.asmx" ... />
</client>
</system.serviceModel>
This application is often used on fairly locked down Windows systems, such that administrator rights are required (typically via UAC) to modify any files within the Program Files directory in which the application is installed. That is fine for most configuration, as it should not be user configurable.
I would like to relocate the endpoint configuration to the user's app data folder, so that this can be modified at a later date, post installation, and without any administrator intervention.
I understand I can use the configSource attribute of the client element under system.serviceModel to point to a separate config file however this does not seem to support absolute paths (let alone ideally also the appdata environment variable), and hence does not appear to be an option for pointing to appdata.
My question therefore is, how can I relocate this configuration block to an external file in the user's appdata folder where it can be modified without subsequent administrator intervention whilst leaving the rest of the configuration securely in the program files installation folder?
You need "externalize" your <client> section like this:
Move the contents of your <client> section to a new file, e.g. `~/App_Data/Config/Client.config":
<client>
<endpoint address="http://blahblah/service.asmx" ... />
</client>
Modify our existing config to point to that externalized config file:
<system.serviceModel>
<bindings>
...
</bindings>
<client configSource="/App_Data/Config/Client.config" />
</system.serviceModel>
Now, you can modify our client settings in that external file, without having to touch the main web.config file anymore.
Just word of caution: while changing the web.config also automatically causes the IIS application pool to recycle (and thus reload the new config), this is no longer the case when you modify such an externalized config file. So you'll need to explicitly recycle the IIS app pool to get the new settings reloaded from your config file
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 !
I'm creating a webservice client. And I have the problem - in MainWindow.xaml there is a message: 'Could not find endpoint element with name 'LightsWSSoap' and contract 'LightsWS.LightsWSSoap' 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.'
In this file I have something like:
xmlns:vm="clr-namespace:LightsClient2.ViewModels"
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
And in the MainWindowViewModel the constructor of webservice is used:
LightsWSSoap lService = new LightsWSSoapClient("LightsWSSoap");
Where 'LightsWS' is the name of the Service.
There is an app.config file and there are enpoints definitions in it:
<client>
<endpoint address="http://xxx/Lights/LightsWS.asmx"
binding="basicHttpBinding" bindingConfiguration="LightsWSSoap"
contract="LightsWS.LightsWSSoap" name="LightsWSSoap" />
<endpoint address="http://xxx/Lights/LightsWS.asmx"
binding="customBinding" bindingConfiguration="LightsWSSoap12"
contract="LightsWS.LightsWSSoap" name="LightsWSSoap12" />
</client>
What is wrong? Any ideas?
You will need to configure your client endpoint in the App.Config file in your WPF application.
If there is not already an App.Config file you can create one by adding a new item to your project and selecting "Application Configuration File".
The contents of App.Config should look something like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint binding="wsHttpBinding" contract="LightsWS.LightsWSSoap" name="LightsWSSoap" />
</client>
</system.serviceModel>
</configuration>
There is a lot more detailed information on MSDN - http://msdn.microsoft.com/en-us/library/ms731745%28VS.90%29.aspx.
You should add app.config file with definition of your endpoint. Usually it is create by Visual Studio. But if you created your ServiceReference in another assembly (for example, is separate dll), then just copy app.config from that assembly to your WpfApplication.
Try the ideas in this thread, especially the namespaces.
Any ideas how to fix this?
UserService.UserServiceClient userServiceClient = new UserServiceClient();
userServiceClient.GetUsersCompleted += new EventHandler<GetUsersCompletedEventArgs>(userServiceClient_GetUsersCompleted);
userServiceClient.GetUsersAsync(searchString);
.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_UserService"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:52185/UserService.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_UserService"
contract="UserService.UserService"
name="BasicHttpBinding_UserService" />
</client>
<behaviors>
<serviceBehaviors>
<behavior name="Shell.Silverlight.Web.Service3Behavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service behaviorConfiguration="Shell.Silverlight.Web.Service3Behavior"
name="Shell.Silverlight.Web.Service3">
<endpoint address=""
binding="basicHttpBinding"
contract="Shell.Silverlight.Web.Service3" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
Could not find default endpoint element that references contract 'UserService.UserService' 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 contract could be found in the client element.
Resolved!
I didn't mention that this was a Silverlight application. I had the wcf reference in a DLL which had it's own "ServiceReferences.ClientConfig" file. I moved the contents of the DLL's ServiceReferences.ClientConfig to the main silverlight project and it worked.
I had a run in with the same problem. My application was also a Silverlight application and the service was being called from a class library with a custom UserControl that was being used in it.
The solution is simple. Copy the endpoint definitions from the config file (e.g. ServiceReferences.ClientConfig) of the class library to the config file of the silverlight application. I know you'd expect it to work without having to do this, but apparently someone in Redmond had a vacation that day.
You can also set these values programatically in the class library, this will avoid unnecessary movement of the config files across the library.
The example code for simple BasciHttpBinding is -
BasicHttpBinding basicHttpbinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
basicHttpbinding.Name = "BasicHttpBinding_YourName";
basicHttpbinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
basicHttpbinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
EndpointAddress endpointAddress = new EndpointAddress("http://<Your machine>/Service1/Service1.svc");
Service1Client proxyClient = new Service1Client(basicHttpbinding,endpointAddress);
Just in case anyone hits the same problem whilst using WPF (rather than WCF or Silverlight):
I had this error, when connecting to a Web Service. When my code was in the "main" WPF Application solution, no problem, it worked perfectly. But when I moved the code to the more sensible DAL-layer solution, it would throw the exception.
Could not find default endpoint element that references contract 'MyWebService.MyServiceSoap' 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 contract could be found in the client element.
As has been stated by "Sprite" in this thread, you need to manually copy the tag.
For WPF apps, this means copying the tag from the app.config in my DAL solution to the app.config in the main WPF Application solution.
I ran into the same issue, for whatever reason Visual Studio did not update the web config when I first added the service. I found that updating the Service Reference also fixed this issue.
Steps:
Navigate to the Service Reference Folder
Expand it
Right Click and Select update Service Reference
Observe web Config be updated
Change the web.config of WCF service as "endpoint address="" binding="basicHttpBinding"..." (previously binding="wsHttpBinding")After build the app, in "ServiceReferences.ClientConfig" ""configuration> has the value. Then it will work fine.
Rename the output.config produced by svcutil.exe to app.config.
it worked for me.
Do you have an Interface that your "UserService" class implements.
Your endpoints should specify an interface for the contract attribute:
contract="UserService.IUserService"
Not sure if this is an issue.
Endpoint and binding both have the same name
Not sure if it's really a problem, but I see you have the same name for your binding configuration ().
I usually try to call my endpoints something like "UserServiceBasicHttp" or something similar (the "Binding" really doesn't have anything to do here), and I try to call my binding configurations something with "....Configuration", e.g. "UserServiceDefaultBinding", to avoid any potential name clashes.
Marc
Had to add the service in the calling App.config file to have it work. Make sure that you but it after all . This seemed to work for me.
This problem occures when you use your service via other application.If application has config file just add your service config information to this file.
In my situation there wasn't any config file so I use this technique and it worked fine.Just store url address in application,read it and using BasicHttpBinding() method send it to service application as parameter.This is simple demonstration how I did it:
Configuration config = new Configuration(dataRowSet[0]["ServiceUrl"].ToString());
var remoteAddress = new System.ServiceModel.EndpointAddress(config.Url);
SimpleService.PayPointSoapClient client =
new SimpleService.PayPointSoapClient(new System.ServiceModel.BasicHttpBinding(),
remoteAddress);
SimpleService.AccountcredResponse response = client.AccountCred(request);
For those who work with AX 2012 AIF services and try to call there C# or VB project inside AX (x++) and suffer from such errors of "could not find default endpoint"... or "no contract found" ...
go back to your visual studio (c#) project and add these lines before defining your service client, then deploy the project and restart AX client and retry:
Note, the example is for NetTcp adapter, you could easily use any other adapter instead according to your need.
Uri Address = new Uri("net.tcp://your-server:Port>/DynamicsAx/Services/your-port-name");
NetTcpBinding Binding = new NetTcpBinding();
EndpointAddress EndPointAddr = new EndpointAddress(Address);
SalesOrderServiceClient Client = new SalesOrderServiceClient(Binding, EndPointAddr);
In case if you are using WPF application using PRISM framework then configuration should exist in your start up project (i.e. in the project where your bootstrapper resides.)
In short just remove it from the class library and put into a start up project.