Unable to find element in WCF ServiceModel client configuration section - c#

[ServiceContract]
public interface IService1
{
[OperationContract]
DataTable GetADUserList(string strUserName, string strFirstName, string strLastName, string strEmail, string domain);
}
I have a WCF service hosted in IIS with the sample service contract above. The service web.config file settings are as below.
Full WCF Web.config file
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="notSecureBinding">
<binaryMessageEncoding />
<httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
</binding>
<binding name="SecureBinding">
<binaryMessageEncoding />
<httpsTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://ServerName.myDomain.org/ADSearcher/Service1.svc"
binding="customBinding"
bindingConfiguration="notSecureBinding"
contract="ADSearcher.IService1"
name="notSecureBinding" />
<endpoint address="http://ServerName.myDomain.org/ADSearcher/Service1.svc"
binding="customBinding"
bindingConfiguration="SecureBinding"
contract="ADSearcher.IService1"
name="SecureBinding" />
</client>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
And i'm trying to access the service programmatically as below.
EndpointAddress endpointAddress = new EndpointAddress("http://ServerName.myDomain.org/ADSearcher/Service1.svc");
IService1 ADUser = new ChannelFactory<IService1>("notSecureBinding", endpointAddress).CreateChannel();
The above code is throwing the error below
Could not find endpoint element with name 'notSecureBinding' and contract 'ADSearcher.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 name could be found in the client element
I can't seem to figure out what i'm doing wrong here or is there a better alternative to access this service programmatically?

In your endpoint, you are specifying a contract of type Data.GetData. The contract is of type IService1, and by default, the contract name should be the typename of the service interface.
If you really want your IService to be referred to as Data.GetData, you can specify the identifying name via the ServiceContractAttribute:
[ServiceContract(ConfigurationName = "Data.GetData")]
public interface IService1
{
[OperationContract]
DataTable GetADUserList(string strUserName, string strFirstName, string strLastName, string strEmail, string domain);
}

Could not find endpoint element with name 'customBinding'....in the
ServiceModel client configuration section
This is because you are defining two client endpoints in your config file. And they are named:
notSecureBinding, and
SecureBinding
So the error message is correct, there is no endpoint element with name "customBinding".
I think you need to do this:
IService1 ADUser = new ChannelFactory<IService1>("<either of the two bindings you defined>").CreateChannel();
Do i need to configure any thing related to WCF in the ASP.NET MVC
application's web.config file?
OK so now I understand. You are hosting the WCF service in your ASP.NET website. And you're trying to call it from somewhere else I assume?
You definitely need to define a <system.serviceModel /> section in your web.config, which will tell IIS how to host your service.
At the moment, the configuration you gave posted above defines to client endpoints. These do not reference service endpoints, but rather is client config designed to allow you to call services. The hint is in the name of the section node: <client />. To define service endpoints that you wish to expose you need to put your endpoint config in a <service /> section. In your case it may only be necessary to change the "client" to "service" to make it all work.
The code which is calling the service may or may not need client configuration in the app.config file. You say you are calling it programatically (although without seeing your full client code I don't know that you are doing enough to satisfy the WCF client call stack), in which case you don't need client config. I hope this makes things clearer for you.

Related

Set WCF Service endpoints at server side web config for https

I am stuck in very odd situation and do not know how to solve this issue.
I have very simple WCF service working very fine with http BUT now I want to move it to https.
The issue is Server where it needs to be hosted. Server IIS have an alias name "server-test.local" (not "localhost") with http. Its is accessible locally with this alias name. And its mapped to a Domain name with https (example, https://rmt.XXXXXXX.com).
When I deploy my WCF to that server and check WSDL file, it has mapping of the local alias name instead of public domain name.
it is showing http://server-test.local/WCFService/Service.svc (WRONG)
I want mapping like https://rmt.XXXXXXXX.com/WCFService/Service.svc (CORRECT)
Service web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 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>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer> <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
When I add service reference to app. It adds following endpoint which is not conrrect.
Client App.config (endpoint)
<endpoint address="http://server-test.local/WCFService/Service.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IProcessing"
contract="WCFService.IService" name="BasicHttpBinding_IProcessing" />
I am able to access the url and WSDL file through browser but not able to consume any Service methods. I am getting error "There is no endpoint which can accept the message."
I think I am missing something obvious here. Please suggest me changes.
You can try to change some simple steps in the web.config file, you can refer to this post for details.
1.Enable transport level security in the web.config file of the service:
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
2.Use the bindingConfiguration tag to specify the binding name and the behaviorConfiguration tag to configure the behavior, and then specify the address of the managed service. The code is as follows:
<service name="***" behaviorConfiguration="***">
<endpoint address= https://rmt.XXXXXXXX.com/WCFService/Service.svc
binding="basicHttpBinding"
bindingConfiguration="???"
contract="WCFService.IService"/>
<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
</service>

System.Net.WebException when trying to consume WCF service

I am developing system composed of WCF service and Xamarin.Forms client app. Seems like my application connects to server just fine (client has status Open when I check before invoking any methods), but after I try to invoke a service method, I am getting System.Net.WebException:
There was no endpoint listening at {0} that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
And the inner exception is:
System.Net.WebException: There was an error on processing web request: Status code 404(NotFound): Not Found
I can access service via web browsers on both pc and phone with address http://localhost:4408/DatabaseService.svc or http://192.168.8.106:4408/DatabaseService.svc but I get the exception when I am trying to invoke any methods from my app.
My client app is connecting to the host and invokes methods like this:
public App()
{
var binding = new BasicHttpBinding();
var timeout = new TimeSpan(0, 0, 30);
binding.SendTimeout = timeout;
binding.ReceiveTimeout = timeout;
dbClient = new DatabaseServiceClient(binding, new EndpointAddress("http://192.168.8.106:4408/DatabaseService.svc"));
dbClient.Test();
}
My service is hosted by this simple program:
class Program
{
static void Main()
{
Uri baseAddress = new Uri("http://192.168.8.106:4408/DatabaseService.svc");
ServiceHost selfHost = new ServiceHost(typeof(DatabaseService.DatabaseService), baseAddress);
try
{
selfHost.AddServiceEndpoint(typeof(DatabaseService.IDatabaseService), new WSHttpBinding(), "DatabaseService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior
{
HttpGetEnabled = true
};
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
Console.WriteLine("Host Status: " + selfHost.State);
Console.WriteLine("Host Addresses: " + selfHost.BaseAddresses.Count);
foreach (var x in selfHost.BaseAddresses)
Console.WriteLine(" - " + x.AbsoluteUri);
Console.WriteLine("<q to close >");
string command = "";
while (command != "q")
command = Console.ReadLine();
selfHost.Close();
Console.WriteLine("Host shutdown");
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Exception thrown:\n" + e.Message);
Console.ReadLine();
selfHost.Abort();
}
}
}
Part of my applicationhost.config edited by me is:
<site name="OutdoorGame" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="F:\Studia\Magisterka\Github\Serwer\OutdoorGame\OutdoorGame" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:4408:localhost" />
<binding protocol="http" bindingInformation="*:4408:127.0.0.1" />
<binding protocol="http" bindingInformation="*:4408:192.168.8.106" />
</bindings>
</site>
I tried to stick to the tips included here: https://learn.microsoft.com/pl-pl/xamarin/xamarin-forms/data-cloud/web-services/wcf and I should also mention, that my services are working just fine, when accesed via ISS Express WCF Test Client.
I think I am missing something very simple but I have no clue what would it be. Any help will be much appreciated.
EDIT1
I probably should also show part of Web.config of the server:
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttp"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferPoolSize="524288"
maxReceivedMessageSize="65536"
messageEncoding="Text"
textEncoding="utf-8"
useDefaultWebProxy="true"
allowCookies="false">
<security mode="Transport">
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" 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="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<services>
<service name="DatabaseService.DatabaseService">
<endpoint
address="http://192.168.8.106:4409/DatabaseService.svc"
binding="basicHttpBinding" bindingConfiguration="basicHttp"
contract="DatabaseService.IDatabaseService"/>
</service>
</services>
<protocolMapping>
<add scheme="http" binding="basicHttpBinding" bindingConfiguration="basicHttp"/>
</protocolMapping>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
</system.webServer>
If you have configured the endpoint information in the configuration file, you do not need to use the hosting program to configure the endpoint information. Moreover, if the project created using the WCF service application template does not require a program to host it, it can be directly deployed to IIS.
This project can be deployed directly to IIS or run directly in VS.
Secondly, I suggest you use Add Service Reference to generate the client:
Finally, you can directly call the service through the automatically generated proxy class.
Okey, so since I don't have much time, I basically gave up on this and just went to previous version of my project. Now my files look like this:
Connecting like this:
{
dbClient = new DatabaseServiceClient(new BasicHttpBinding(), new EndpointAddress("http://192.168.8.106:13409/DatabaseService.svc"));
}
applicationhost.config
<site name="DatabaseService" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="F:\Studia\Magisterka\Github\Serwer\OutdoorGame\DatabaseService" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:13409:localhost" />
<binding protocol="http" bindingInformation="*:13409:192.168.8.106" />
<binding protocol="http" bindingInformation="*:13409:127.0.0.1" />
</bindings>
</site>
My web.config is basically default.
Ports used are different because meanwhile I tried to create a new service (thought that could solve the problem) and that could be the thing.
Thanks to Ding Peng now I know, that I don't need service host, IIS Express is enough to host it.
Thanks guys for help.

Self Hosted WCF Service throws Error Message "405 Method not allowed"

I have a Self Hosted WCF Service, which looks like this:
[ServiceContract]
public interface IService {
[OperationContract]
[WebGet]
List<Data> GetData();
//...and much more Methods
}
My App.config looks like this:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<serviceBehaviors>
<behavior name="MetaInformation">
<serviceMetadata httpGetEnabled="true"
httpGetUrl="http://localhost:8500/MetaInfo"
httpsGetBinding="" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpointBehavior">
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MetaInformation" name="Library.WcfService.ServiceModel">
<endpoint address="http://localhost:8500/Service"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBindingSettings"
contract="Library.WcfService.IService"
bindingName="BasicHttpBindingSettings"
behaviorConfiguration="EndpointBehavior"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBindingSettings"
closeTimeout="00:50:00"
openTimeout="00:50:00"
sendTimeout="00:50:00"
maxBufferSize="524288"
transferMode="Streamed"
maxReceivedMessageSize="2147483647"
maxBufferPoolSize="2147483647"
messageEncoding="Text">
<readerQuotas maxDepth="2147483647"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
<system.web>
<httpRuntime maxRequestLength="102400"/>
</system.web>
When I run this Server and the Client Applications on my local machine, it's working fine.
But when i try to run the Server Application on another PC, I cannot add a Service Reference at the Client because I get this:
405 Method not allowed Metadata contains a reference that can not be resolved : "http://192.168.178.54:8500/MetaInfo". It was not a
listening to http://192.168.178.54:8500/MetaInfo endpoint present who
could accept the message. This is often caused by an incorrect
address or SOAP action
I tried almost everything I found at the internet but nothing worked.
Switching to IIS or using an other Protocol should be a plan B, i want to keep it self hosted with http.
Please can somebody help me I'm desperate with this Problem.
your operation contract is decorated with [WebGet] attribute, which means you're trying to expose your service as a REST. But your service is using basicHttpBinding as means of building the communication channel which isn't supported because the content type for this binding is soap+xml. You'll need to use WebHttpBinding in this case which is the only binding that supports restful implementation of WCF Services and supports both Xml and Json data types.

Configure WCF service server side to accept Mtom

I'm pretty new to web services and I'm trying to configure my WCF service to pull files from another service using the Visual WCF service template.
I've managed to pull everything except for the content data. I've searched and found that I have to use Mtom instead of Text for message encoding. Problem is i'm not sure how to configure my web service to use MessageEncoding = Mtom. I've fiddled with the web.config file but i can't seem to configure it properly.
This is my service interface
[ServiceContract]
public interface IService
{
[OperationContract]
string test(changeData data);
[OperationContract]
RetrieveChangeListResponse GetChangeList();
[OperationContract]
RetrieveChangeTaskListResponse GetTaskList();
[OperationContract]
RetrieveInteractionAttachmentResponse GetFile(string id);
// TODO: Add your service operations here
}
This is my web.config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttp" allowCookies="true"
maxReceivedMessageSize="20000000"
maxBufferSize="20000000"
maxBufferPoolSize="20000000"
messageEncoding ="Mtom">
<readerQuotas maxDepth="32"
maxArrayLength="200000000"
maxStringContentLength="200000000"/>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="IService"
behaviorConfiguration="ServiceWithMetadata">
<endpoint name="BasicHttpBinding_IService"
binding="basicHttpBinding"
bindingConfiguration="basicHttp"
contract="IService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceWithMetadata">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
Currently if use this setup I get this error:
Error: Cannot obtain Metadata from localhost:50282/Service.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at URI: localhost:50282/Service.svc Metadata contains a reference that cannot be resolved: 'localhost:50282/Service.svc'. Content Type application/soap+xml; charset=utf-8 was not supported by service localhost:50282/Service.svc. The client and service bindings may be mismatched. The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..HTTP GET Error URI: localhost:50282/Service.svc The HTML document does not contain Web service discovery information.
If I remove these settings I get a type mismatch like this:
Client found response content type of 'multipart/related; type="text/xml"; boundary="----=_Part_0_30957184.1421681490926"', but expected 'text/xml'.
I understand there is a config for the WcfTestClient and a config for the actual service, but I don't understand how to properly configure my server side to properly accept Mtom encoding.
Could someone explain how to properly configure the server side binding ? I use BasicHttpBinding, but would also like to use wshHttpBinding for soap 1.2 functionality.
Thanks in advance.
The problem was in the the authentification method used, i was using annonymous instead of basic.
Also you can't have method return types that are proxy data types. If you have such a method you will get an error about class derivation.

Hosting NamedPipe WCF on Win7 IIS 7.5 EndpointNotFoundException

I am trying to create WCF NamedPipe in win7 with IIS 7.5.
In IIS Manager right click, and create WebSite named "TestWCFWithNamedPipe"
Right click my WebSite "TestWCFWithNamedPipe" -> select Edit bindings
Type:http
Host Name:sample.localdev.net
Port:80
Address:*
Type:net.pipe
Binding informations:*
In Advanced settings set the value for Enabled Protocol as "http,net.pipe"
In My WebSite "TestWCFWithNamedPipe" Web Application Project
web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="DefaultBehavior">
<serviceDebug />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DefaultBehavior" name="SampleWcfLib.SampleWcfObj">
<endpoint address="net.pipe://localhost/Sample" binding="netNamedPipeBinding"
bindingConfiguration="" contract="SampleWcfInterfaceLib.ISampleWcfObj" />
</service>
</services>
</system.serviceModel>
TestClient.exe code is following
string address = "net.pipe://localhost/Sample";
NetNamedPipeBinding binding = new NetNamedPipeBinding();
EndpointAddress ep = new EndpointAddress(address);
ISampleWcfObj channel = ChannelFactory<ISampleWcfObj>.CreateChannel(binding, ep);
string result = channel.Ping("Test");
And I run TestClient.exe, and I get an exception:
There was no endpoint listening at net.pipe://localhost/Sample that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
I have checked Win7 Service status:
Net.Pipe Listener Adapter Started
Net.Tcp Listener Adapter Started
I have done all settings for WCF.
Why do I still get the EndpointNotFoundException error message ?
You can find the name of your tcp binding if you have a look in your XSD file.
It will be on the bottom of your file and it will look like this
<wsdl:service name="PersonService">
<wsdl:port name="NetNamedPipeBinding_IPersonContract" binding="tns:NetNamedPipeBinding_IPersonContract">
<soap12:address location="net.pipe://wcftestpipe/WcfTest/PersonService.svc/test"/>
<wsa10:EndpointReference>
<wsa10:Address>net.pipe://wcftestpipe/WcfTest/PersonService.svc/test</wsa10:Address>
</wsa10:EndpointReference>
</wsdl:port>
</wsdl:service>
Afther that you propably get a security exeception that will say you don't have the right credentials. You can set your binding security mode to NONE in your web.config of your WCF service and app.config of your Client.
<bindings>
<netNamedPipeBinding>
<binding>
<security mode="None"/>
</binding>
</netNamedPipeBinding>
</bindings>
Hope this helps

Categories