Could not find default endpoint element that references contract - c#

Mine is windows application. I am using web service. When i add service reference to my application it is generated one app.config file.
<client>
<endpoint address="http://58.71.131.223/uucpapi/UUCPAPIServer.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IUUCPAPIServer"
contract="UUCPAPI.IUUCPAPIServer" name="WSHttpBinding_IUUCPAPIServer">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
and i am calling this API as
UUCPAPI.UUCPAPIServerClient uucp = new UUCPAPI.UUCPAPIServerClient();
here it is throwing exception as
Could not find default endpoint element that references contract
'UUCPAPI.IUUCPAPIServer' 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."

<client>
<endpoint address="http://...../uucpapi/UUCPAPIServer.svc"
binding="wsHttpBinding"
binding configuration="WSHttpBinding_IUUCPAPIServer"
contract="UUCPAPI.IUUCPAPIServer"
name="WSHttpBinding_IUUCPAPIServer">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>

Related

Service metadata may not be accessible

I am trying to run my project.I am getting this error "Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata".
add this endpoint to your application.config file:
<services>
<service .....>
<endpoint binding="mexHttpBinding" bindingConfiguration="" name="mexService" contract="IMetadataExchange" />
<endpoint ...... the usual endpoints />
</service>
</services>

Get DNS entry of localhost for service

For a IIS hosted service I have to get the full address of the local machine.
In the Web.config that value is perfectly configured
<services>
<service name="MyService.Service.MyService">
<endpoint address="" behaviorConfiguration="MexFlatWsdlBehavior"
binding="basicHttpBinding" bindingConfiguration="secureHttpBinding"
name="TpsNotification" bindingNamespace="http://org.com/MyService/2014/11"
contract="MyService.Service.ISomeEndpoint">
<identity>
<dns value="THESERVER.myorg.net" />
</identity>
</endpoint>
<endpoint address="" behaviorConfiguration="MexFlatWsdlBehavior"
binding="basicHttpBinding" bindingConfiguration="secureHttpBinding"
name="TpsPluginFramework" bindingNamespace="http://org.com/MyService/2014/11"
contract="MyService.Service.IOtherEndpoint">
<identity>
<dns value="THESERVER.myorg.net" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="https://THESERVER.myorg.net:443/MyService.svc" />
</baseAddresses>
</host>
</service>
</services>
Any of dns value or baseAddress would be perfectly fine.
But I see no way to get to that value from IIS.
I tried ConfigurationManager and WebConfiguration and HostingEnvironment.
Furthermore I tried
var hostName = System.Net.Dns.GetHostName();
which works for some cases, but not for this server, because the configured address is not the primary dns address, which is returned here.
Any options?
Try loading the config file in to a linq to xml doc, then you can query it all you want ...
var config = XDocument.Load("Web.Config");
var baseAddress = config.Elements("baseAddresses")
.Elements()
.First()
.Attribute("baseAddress")
.Value;
If all else fails, use the brute force approach :)
Another option might be ...
Since you are in the context of a request to a server why not use the request object ...
var host = Request.Url.Host;
That was simpler!

Web.config transformation in smart way

I have client endpoints as below in my web.config, we have production server located geographically for e.g. America, Europe, Asia etc and all have their own base uri.
e.g. web.config is as below
<client>
<endpoint address="http://localhost:9000/ServiceA.svc" binding="wsHttpBinding" contract="IServiceA" name="ServiceA"></endpoint>
<endpoint address="http://localhost:9000/ServiceB.svc" binding="wsHttpBinding" contract="IServiceA" name="ServiceB"></endpoint>
<endpoint address="http://localhost:9000/ServiceC.svc" binding="wsHttpBinding" contract="IServiceA" name="ServiceC"></endpoint>
<endpoint address="http://localhost:9000/ServiceD.svc" binding="wsHttpBinding" contract="IServiceA" name="ServiceD"></endpoint>
<endpoint address="http://localhost:9000/ServiceE.svc" binding="wsHttpBinding" contract="IServiceA" name="ServiceE"></endpoint>
For Ameria region I have Web.America.config but it should be transformed as below
<client>
<endpoint xdt:Transform="SetAttributes(address)" xdt:Locator="Match(name)" address="http://localhost:9000/america/ServiceA.svc" name="ServiceA"></endpoint>
<endpoint xdt:Transform="SetAttributes(address)" xdt:Locator="Match(name)" address="http://localhost:9000/america/ServiceB.svc" name="ServiceB"></endpoint>
...
...
But I don't want this duplication as I only need to change base address. Is there any better way of doing this?

WCF Self Hosting timeout

I have a Unity based C# WPF application that has a Module that communicates to a WCF based project within the same solution.
The WCF dll works if i use the WCF test host make it an active service, but times out if i try to create a ServiceHost from code.
here is the code that is in the Unity Modules Initialization()
host = new ServiceHost(typeof(AtomCfgModelClient));
host.Open();
Here is my app.config section
<endpoint address="http://localhost:8080/AtomCfgModelService"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IAtomCfgModel"
contract="AtomCfgModel.IAtomCfgModel" name="WSHttpBinding_IAtomCfgModel">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
<services>
<service name="AtomCfgModule.AtomCfgModel.AtomCfgModelClient" >
<endpoint address="" binding="wsHttpBinding" contract="AtomCfgModel.IAtomCfgModel" >
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/AtomCfgModelService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior >
<serviceMetadata httpGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
ive turned off "start WCF Service Host when debugging " from the wcf project properties setting.
So, the above configuration seems to load ok from the wcf trace file, but when i go to use any of the services, i get a timeout without any information that i can see that lets me know what is causing the timeout.
here is the error message from the wcf trace, prior to this exception message, all the messages seem to succeed.
The HTTP request to 'http://localhost:8080/AtomCfgModelService' has exceeded the allotted timeout of 00:00:59.9840000. The time allotted to this operation may have been a portion of a longer timeout.
What is confusing is that if i use the WCF test host, then the above configuration works
String arg = "/service:\"" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AtomCfgModel.dll") + "\" /config:\"" + Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GISShell.exe.config") + "\"";
System.Diagnostics.Process.Start(#"c:\WcfSvcHost.exe", arg);
The above code running the wcfsvchost.exe uses the same config ( and WCF dll ) and i dont get the timeout.
Any points on how to debug/fix this would great.

Indexing ServiceEndpointElementCollection

In the following config file excerpt, the WCF service has two endpoints.
<service behaviorConfiguration="AtomTcpHub.Behavior"
name="AtomTcpHub.HubTcp">
<endpoint address="" binding="netTcpBinding"
name="AtomHubEndpoint" contract="AtomLib.IAtomPublisher">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
name="" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/AtomTcpHub/" />
<add baseAddress="net.tcp://dv-pw/AtomTcpHub/" />
</baseAddresses>
</host>
</service>
In my code there is discovery logic, which responds to a UDP request by replying with the connection Uri for the WCF service. Obtaining a collection of endpoints is straightforward.
System.Configuration.Configuration config = System.Configuration
.ConfigurationManager
.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
ServicesSection section = config.SectionGroups["system.serviceModel"]
.Sections["services"] as ServicesSection;
ServiceEndpointElementCollection seec =
section.Services["AtomTcpHub.HubTcp"].Endpoints;
The problem is extracting the ServiceEndpointElement. We can have it by index:
ServiceEndpointElement see = seec[0];
but this is brittle; if the order of nodes changes it will break. Visual Studio tells me there is another indexer permitting an object value, but there is no further indication. Experimentation tells me that it isn't the value of the name attribute.
The following code works, but it's just hideous.
string serviceEndpointUri;
foreach(ServiceEndpointElement serviceEndpointElement in seec)
if (serviceEndpointElement.Name == "AtomHubEndpoint")
{
_serviceEndpointUri = serviceEndpointElement.Address.AbsoluteUri;
break;
}
Is there a more direct or more elegant way to do this?
You could always use some Linq to accomplish this, to simply shorten things a bit.
ServiceEndpointElement element =
seec.OfType<ServiceEndpointElement>()
.FirstOrDefault(s => s.Name == "AtomHubEndpoint");

Categories