load wcf hosted on console app with app.config? - c#

Hello im making wcf hosted in console app is possible to load this service with app.config?
ChannelFactory<IService> service = new ChannelFactory<IService>();
service.Endpoint.Behaviors.Add(new WebHttpBehavior());
IService channel = service.CreateChannel();
Console.Read();
This is start of my wcf into console app but how to load wcf config?
Im new in wcf so please be polite for me.
--edit---
So i get errorr
s zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.
ServiceHost serviceHost = null;
serviceHost = new ServiceHost(typeof(Contracs.Service));
serviceHost.Open();
and my app.config is
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="restfulBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Contracs.Service">
<endpoint address="" behaviorConfiguration="restfulBehavior"
binding="webHttpBinding" bindingConfiguration="" contract="Contracs.IService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/Service" />
</baseAddresses>
</host>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

WCF will by default try to load from the app config.
Here is some more info:
http://msdn.microsoft.com/en-us/library/ms733932(v=vs.110).aspx

Related

WCF AJAX enabled service created OK, but method returns 404

This is my first WCF service, so please be understanding. :)
I have an existing ASP.Net web site that has been running OK so far.
Using VS 2017 I added a new WCF Ajax enabled service.
VS created the .svc and .cs files and it also made cahnges to the web.config.
I made sure that HTTP activation is enabled in server roles.
When navigating to the service url via browser, the service page loads. OK.
When I try to add "/DoWork" to access the method that was created as the default, i get a 404 error.
I have been going CRAZY trying different tutorial suggestions. NOTHING WORKS.
I must be missing something simple. Something maybe is not enabled at server level?
I would think that the WCF code that was added by VS would work as it, right?
WHY 404?????
What am I missing????
Thank you for any help and best regards,
Udar
If you use webhttpbinding, you need to add webHttp to the endpoint behavior:
<endpointBehaviors>
<behavior name="ESEndPointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
This is my configuration file, and I enabled the help document:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<system.serviceModel>
<services>
<service name="Demo_rest_ConsoleApp.Service1" behaviorConfiguration="ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8763/TEST/"/>
</baseAddresses>
</host>
<endpoint address=""
binding="webHttpBinding"
contract="Demo_rest_ConsoleApp.IService1" behaviorConfiguration="ESEndPointBehavior"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="ESEndPointBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceThrottling maxConcurrentCalls="1" maxConcurrentInstances="1" maxConcurrentSessions="1"/>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Feel free to let me know if the problem persists.
Here is the relevant section of web.config
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="NativeAppConnectorWCFAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="NativeAppConnectorWCF">
<endpoint address="" behaviorConfiguration="NativeAppConnectorWCFAspNetAjaxBehavior"
binding="webHttpBinding" contract="NativeAppConnectorWCF" />
</service>
</services>
</system.serviceModel>

Host HTTP WCF Service in a Windows Service

I'm hosting a HTTP WCFService in a Windows Service, in local network it works perfectly, but if the client is in another network and try to connect with de public IP doesn't work.
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="WCFService.ServiceBehavior">
<endpoint address="" binding="wsHttpBinding"
contract="WCFService.ServiceContract">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:80/WCFService/service/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
The service metadata publishes http://localhost:80/WCFService/service/ to the client. This URL is not accessible from outside the local host.
In order to access the service from another network using a public IP the service metadata should publish http://PUBLIC_IP_ADDRESS/WCFService/service/ to the client. This can be done dynamically depending on the URL used by the client. Just add useRequestHeadersForMetadataAddress to service behaviors.
<behaviors>
<serviceBehaviors>
<behavior name="...">
...
<useRequestHeadersForMetadataAddress />
</behavior>
</serviceBehaviors>
</behaviors>
See Auto-resolving a hostname in WCF Metadata Publishing.
I suspect that when you give a config like this:
<add baseAddress="http://localhost:80/WCFService/service/" />
It would be listening in the loopback address due to the usage of locahost. Change this to the actual public IP address (i.e., not 127.0.0.1) or Server name and check again.

Bind self-hosted WCF service to subdomain

I have 2 instances of a self-hosted wcf service. One dev and one acceptance.
These need to be bound to 'dev.example.com/TestService' and 'acc.example.com/TestService'.
When I start the first service, it binds to 'http://+:80/TestService'.
Requests for both environments are received on the first instance.
And when the second instance is started, it can't bind anymore.
Is there a way to configure the servicehost to only bind on specific subdomain?
Config:
<system.serviceModel>
<services>
<service name="TestProject.TestService" behaviorConfiguration="WebServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://dev.example.com/TestService" />
</baseAddresses>
</host>
<endpoint binding="basicHttpBinding" contract="TestProject.ITestService">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WebServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Code:
_host = new ServiceHost(typeof (TestService));
_host.Open();

Specifying runtime address for WCF service via config

I've created a WCF library using VS2010 C#, made reference to it from other project, everithing works fine on designtime. But on runtime, as I understand it, it uses different address. So, how can I specify it?
My App.Config looks like:
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service name="WcfService.Service1">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfService/MyWcf/" />
</baseAddresses>
</host>
<endpoint address ="" binding="wsHttpBinding" contract="WcfService.IService1">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

Why does my WCF service give the message 'does not have a Binding with the None MessageVersion'?

I have created a working WCF service. I now want to add some security to it to filter Ip Addresses. I have followed the example that microsoft publish in the samples to try and add a IDispatchMessageInspector that will raise a call AfterReceiveRequest and then throw an error if the ip address is not from the allowed list.
After looking at the code; they have configured it using 'wsHttpBinding', however I want to use 'webHttpBinding' or 'basicHttpBinding'. But when I set it up I get the error:
The endpoint at 'http://upload/api/Api.svc/soap' does not have a
Binding with the None MessageVersion.
'System.ServiceModel.Description.WebHttpBehavior' is only intended for
use with WebHttpBinding or similar bindings.
My configuration is:
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true">
</serviceHostingEnvironment>
<!--Set up the service-->
<services>
<service behaviorConfiguration="SOAPRESTDemoBehavior" name="HmlApi">
<endpoint address="rest" binding="webHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="SOAPRESTDemoEndpointBehavior" />
<endpoint address="soap" binding="basicHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="SOAPRESTDemoEndpointBehavior" />
</service>
</services>
<!--Define the behaviours-->
<behaviors>
<serviceBehaviors>
<behavior name="SOAPRESTDemoBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<!---Endpoint -->
<endpointBehaviors>
<behavior name="SOAPRESTDemoEndpointBehavior">
<ipFilter/>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="ipFilter" type="VLSCore2.Api.IpFilterBehaviourExtensionElement, VLSCore2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
So what im wondering is how I can set up my message inspector without using WebHttpBinding. Is this even possible?
I want to use SOAP 'basicHttpBinding' not wsHttpBinding (and all of the WS*) associated overheads....
This is simply happening because you have configured a single endpointBehavior for both the SOAP and REST endpoints but the SOAP endpoint can't have the webHttp behavior. You need to split these apart so that they are:
<endpointBehaviors>
<behavior name="SOAPDemoEndpointBehavior">
<ipFilter/>
</behavior>
<behavior name="RESTDemoEndpointBehavior">
<ipFilter/>
<webHttp />
</behavior>
</endpointBehaviors>
and then your endpoints should be:
<endpoint address="rest" binding="webHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="RESTDemoEndpointBehavior" />
<endpoint address="soap" binding="basicHttpBinding" contract="VLSCore2.Interfaces.IHmlApi" behaviorConfiguration="SOAPDemoEndpointBehavior" />
For me, this was because I had a 'webHttp' defined as a behaviour for a SOAP config. Only its absence resolved it..
For 4 days, I have been up and down through MS documentation, Stack overflow and everything else. trying to get the configuration file working correctly after I started using HTTPS / SSL for requests from my service. The setup of this configuration file is so critical.
I did not have to modify any of the code switching from HTTP to HTTPS is was all in the web.config file system.serviceModel section.
This config works for using javascript to access JSON data from the ASP.NET c# WCF. I am so happy!
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false">
</serviceHostingEnvironment>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata 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>
<bindings>
<webHttpBinding>
<binding name="sfbSecureBinding">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="buildingsWebService.Service1">
<endpoint address="https://www.azsfb.gov/Service/Service1.svc"
binding="webHttpBinding"
bindingConfiguration="sfbSecureBinding"
contract="buildingsWebService.IService1"
behaviorConfiguration="webBehavior">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="https://www.azsfb.gov/"/>
</baseAddresses>
</host>
</service>
</services>
<protocolMapping>
<add binding="basicHttpBinding" scheme="https"/>
</protocolMapping>
</system.serviceModel>

Categories