Class library error, calling Webservice - c#

I have a class library, which calls into a Webservice. When i try and instantiate a new instance of the client, I am being thrown the following error is VS:
An exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll but was not handled in user code
Additional information: Could not find default endpoint element that references contract 'SanctionsCheckingService.ISanctionsCheckingService' 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.
My app.config file contains the following, which looks correct:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ISanctionsCheckingService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:56200/SanctionsCheckingService"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISanctionsCheckingService"
contract="SanctionsCheckingService.ISanctionsCheckingService"
name="BasicHttpBinding_ISanctionsCheckingService" />
</client>
</system.serviceModel>
Can anyone point to why this is happening?

Related

My wcf client side shows exception but WcfTestClient Tool works fine

I have coded a WCF server side with netTcpBinding. Then I coded a client side. But it shows exception while excute "var sc = new CommondServiceClient();" at runtime. What should I do?
Below is the exception message:
System.InvalidOperationException
HResult=0x80131509
Message=Could not find default endpoint element that references contract 'ICommondService' 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.
Source=System.ServiceModel
StackTrace:
......
I have tried something:
I can consume the services with WcfTestClient.
The service reference is added by visual studio "add service reference...". I guess I get the service mex data. But I meet runtime exception like above
I also tried generate code with svcutil tool, but it still not work
Here is the wcf config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="ServiceContractor.CommondService">
<endpoint address="" binding="netTcpBinding" contract="ServiceContractor.ICommondService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="False" httpsGetEnabled="False"/>
<!-- 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>
</system.serviceModel>
</configuration>
It's a self host wcf service:
var baseAddress = new Uri($"net.tcp://localhost:{PORT}/Company/service");
_host = new ServiceHost(typeof(CommondService), baseAddress);
try
{
var smb = _host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null) _host.Description.Behaviors.Add(new ServiceMetadataBehavior());
_host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
_host.AddServiceEndpoint(typeof(ICommondService), new NetTcpBinding(), "");
_host.Open();
}
catch (CommunicationException ce)
{
_host.Abort();
}
I have no idea what is wrong. What document I should ask for? Can you help me?
But I have another question now. Is it possible to get rid of the configuration. So that the application just needs the DLL and knows nothing about the WCF service.
Generally speaking, there are two ways to call the WCF service (Soap service) in the web application.
Generate the client proxy class by adding service reference, this way
commonly need to configure the service settings in the configuration
file(web.config), most of service information need to be called are
stored in the configuration file. For class library project, we have
to migrate the configuration to the configuration file of the actual
project.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client
Use the ChannelFactory to create the communication channel, we set
up the service configurations programmatically. From your
description, this way to call the web service might be the
appropriate solution. Customize the service configuration
programmatically in the code. One thing must be noted that all the
service configuration is hard-coded, such as binding type and service
endpoint information. You could refer to the following documents.
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-use-the-channelfactory
Feel free to let me know if there is anything I can help with.

How to connect to WSDL service using Binding Url in .Net

I have been provided a Web Service which i need to consume in my MVC application .
The user has provided
WSDL URL :
http://abc.xyz.nirp.com:50000/dir/wsdl?p=ic/310c503c873138a884ddd3ee4a5738e6
Binding Url:
http://abc123.ad.xyx.com:50000/XISOAPAdapter/MessageServlet?senderParty=&senderService=BS_HARMONY_D&receiverParty=&receiverService=&interface=ABC_OS&interfaceNamespace=http%3A%2F%2Fabc.com%2Fhcm%2Fabc
I am able to add a service reference using the WSDL URL using SOAP credential but i also need to add these in the web configs and app configs which i am not able to understand where to put .
Pointers on how to use this Binding Url and put it in Web Config and App Config which be really helpful for me.
Check your web.config. After adding a service reference visual studio adds the endpoint in your web.config. You probably have a section like that:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="DefaultServiceEndpoint">
<security mode="None" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="ADDRESS_HERE"
binding="wsHttpBinding" bindingConfiguration="DefaultServiceEndpoint"
contract="ServiceReference1.SOME_CLASS" name="DefaultServiceEndpoint" />
</client>
</system.serviceModel>
You can change the address here.

HTTPS WCF Service on Windows Phone 8.0 : Cannot authenticate

i'm experiencing problems on my WP8.0 app, I want to access a HTTPS WCF service from it with BASIC authentication.
Everything is already working on a C# console app which uses .NET framework 4.5
WORKING CONSOLE APP CODE :
App.config :
<?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="WS_CubicusMobileSOAPBinding">
<security mode="Transport">
<transport clientCredentialType="Basic" proxyCredentialType="Basic" realm="xxxxxx.xx.xx" />
</security>
</binding>
</basicHttpsBinding >
</bindings>
<client>
<endpoint address="https://xxx.xxx.xx/xxx/awws/xxxxx.awws"
binding="basicHttpsBinding" bindingConfiguration="WS_CubicusMobileSOAPBinding"
contract="ServiceReference1.WS_CubicusMobileSOAPPortType"
name="WS_CubicusMobileSOAPPort" />
</client>
</system.serviceModel>
</configuration>
And this C# code :
ServiceReference1.WS_CubicusMobileSOAPPortTypeClient proxy = new WS_CubicusMobileSOAPPortTypeClient();
proxy.ClientCredentials.UserName.UserName = "xxxxxx";
proxy.ClientCredentials.UserName.Password = "xxxxxx";
var e = proxy2.ConnexionTestWP(anObject);
NOT WORKING WP8.0 CODE :
ServiceReferences.ClientConfig :
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WS_CubicusMobileSOAPBinding" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://xxxxx.xxxxx.xxx/xxxxx/awws/xxxxx.awws"
binding="basicHttpBinding" bindingConfiguration="WS_CubicusMobileSOAPBinding"
contract="ServiceReference1.WS_CubicusMobileSOAPPortType" name="WS_CubicusMobileSOAPPort" />
</client>
</system.serviceModel>
</configuration>
The C# code is exactly the same than the console app one (except it's Async).
I receive a CommunicationException saying "Additional information: The remote server returned an error: NotFound."
The only thing Fiddler sees :
http://i.stack.imgur.com/VzYhf.png
I heard that Silverlight clients needs crossdomain.xml on client side to work, so I included this file on the server root. Still not working...
I feel like I tried every binding (like custombinding), every c# code (like adding the Authorization header inside the WCF request manually), every kind of configuration etc... Nothing is working.
Please help me out i'm desperate !
You're using basicHttpBinding in Windows Phone but basicHttpsBinding in console app. Have you turned off http on the server?

WCF: Cannot find endpoint reference

I am working on a SharePoint 2013 webpart and a label on the webpart needs to communicate with a SQL Server data table using WCF. I have created the WCF interface and main class, and have also invoked the service in my Visual webpart like this:
protected void Page_Load(object sender, EventArgs e)
{
WcfServiceReference1.Service1Client client = new WcfServiceReference1.Service1Client();
CustomerNameLbl.Text = client.GetCustomerName(ProjectIDDescLbl.Text);
}
Where WcfServiceReference1 is the added WCF service reference and a customer label text is being changed depending on the project number label. The project builds and deploys fine but when I add the webpart, I get this error: Could not find default endpoint element that references contract 'WcfServiceReference1.IService1' 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.
My app.config file is this:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://as-sv-dev02:2345/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="WcfServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
And web.config file (for SharePoint) is this:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://as-sv-dev02:2345/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="WcfServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
Can anyone guide me on how to resolve this? Am I going wrong somewhere?
After a lot of research, I found out that there was some problem with SharePoint which I don't really understand. Thankfully, in time, I chose not to go the SharePoint way and instead work with a simple .aspx page and that would resolve the issue. Just unhappy with Microsoft not offering as much support on such common issues.
I had the same problem and I found this link.
http://social.technet.microsoft.com/Forums/en-US/dea0763d-d6ea-4659-8ef0-8275514d066a/sp2010-consuming-web-service-in-visual-web-part?forum=sharepointdevelopmentprevious
I did it and worked fine. The problem is the deploy does not copy the service model configuration from the Visual Studio' app.config to the IIS's web.config.
I hope helps.
Best Regards.
Your client endpoint does not seem to have a default configuration (name="" or nameless). Try using:
WcfServiceReference1.Service1Client client = new WcfServiceReference1.Service1Client("BasicHttpBinding_IService1");

C# WebService endpoint problem

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.

Categories

Resources