Setting BasicHTTPBinding for WCF service in Visual Web Express 2010 - wcf

I'm creating a WCF service using Visual Web Developer Express 2010. I'd like to try out various bindings for educational purposes.
My memory from 2008 is that the web.config automatically included a section in for <services>, which I then would edit to change the endpoint binding, for example to basicHttpBinding
However my autogenerated 2010 service does not include <services> and any child endpoint or binding details under <system.serviceModel> (see web.config below). Do I need to add this element to the Web.config manually, or is there an alternative way that this should be configured? Or is this a limitation of the Express edition?
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<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>
UPDATE 1 - I've found the following link which seems to describe the same behaviour - investigating now: http://forums.silverlight.net/t/166429.aspx/1

The version of Visual Studio doesn't matter here - it's the version of the .NET framework that is causing your confusion. In .NET 4.0 there are default WCF settings which means that a service can be hosted without any configuration.
MSDN Introduction to WCF 4 will explain more.
If you create the project as a .NET 3.5 project the configuration will be required (and will be added when you 'add new WCF service').
You can add the configuration in .NET 4.0, but if you are new to WCF it's easier if it were automatically generated so you had a starting point to work from.

It seems that WCF 4 creates a default end point if one isn't defined. From http://msdn.microsoft.com/en-us/library/ee354381.aspx
In an effort to make the overall WCF experience just as easy as ASMX,
WCF 4 comes with a new “default configuration” model that completely
removes the need for any WCF configuration. If you don’t provide any
WCF configuration for a particular service, the WCF 4 runtime
automatically configures your service with some standard endpoints and
default binding/behavior configurations. This makes it much easier to
get a WCF service up and running, especially for those who aren’t
familiar with the various WCF configuration options and are happy to
accept the defaults, at least to get started.

Related

How is WCF service connected to endpoints by default?

I have been provided a code base where a WCF service is hosted in IIS. The project uses development server, but that does not matter.
The web.config file contains only the following section for service configuration:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
The .svc file contains the following:
<%# ServiceHost Language="C#" Debug="true" Service="TestApp.MySvcClass" CodeBehind="MySvcClass.svc.cs" %>
MySvcClass implements the contract (ISvcContract).
The thing is, under .NET 4, this configuration works. There is no declaration anywhere that establishes the link between the contract and the service types, there is nothing that links the behaviors to service type, there is no end point declaration, and yet it works.
With .net 3.5, it does not. It complains about the configuration.
What is the default behavior for WCF service projects' configuration?
Where is it documented?
Can I force these type of settings to fail?
I did not have the time to test, but if I add another svc file, the project would probably go crazy, since things may magically fall into place for a single service, but I'd rather not have configuration processed in a smart way.
A Developer's Introduction to Windows Communication Foundation 4
Read the Simplified Configuration section. It lists out default endpoints, protocols, bindings, etc.

Can't define the endpoint after creating my WCF

I wanna learn WCF so I decided to try out creating some application that uses it. Well what I have in mind is I have 2 databases and I wanna create also an SSIS custom data source extension that calls the web service and passes the data (from one database) to an SSIS ADO.Net Data source (into the second database).
Now I created the 2 databases using SQL Server with one table in each. Then I Added a Connection in Visual Studio and then specified the server instance. (didn't create any .mdf files). I added this connectionString in the Web.Config file
<connectionStrings>
<add name="dbconnection" connectionString="
Data Source = SARE-VAIO;
Integrated Security = true;
Initial Catalog = Database1"/>
</connectionStrings>
When I wrote my service which basically populates Database1 with data, I wanted to define the endpoints but when I clicked on the "Edit WCF Configuration" it says 'No Service' is defined? What possibly am I doing wrong here? I want to create an error free service to be able to use it as a source in the SSIS package.
PS. My service has a basicHttpsBinding
UPDATE: I'm using VS 2012 with .Net Framework 4.5
UPDATE 2:
I skipped the endpoint definition for now and went ahead with testing and deploying my WCF. When I invoke the service it says the following error
Failed to invoke the service. Possible causes: The service is offline or inaccessible;
the client-side configuration does not match the proxy; the existing proxy is invalid.
Refer to the stack trace for more detail. You can try to recover by starting a new proxy,
restoring to default configuration, or refreshing the service.
Here is my web.config file
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="dbconnection" connectionString="Data Source = SARE-VAIO; Integrated Security = true; Initial Catalog = Database1"/>
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="false" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
To answer your questions
(1) Why WCF configuration editor shows 'No service is defined' error : Your web.config does not have any services and endpoints defined explicitly (Note: When you host this in IIS you will still get some endpoints added due to the defaults endpoint feature. But config editor tool shows only explicitly defined endpoints). That's the reason the config editor tool shows this message. But you can use the tool to add services and endpoints.
(2) After deploying the service see if the service successfully activated. You can do this by browsing to the metadata URL (your config has metadata enabled). Make sure your service WSDL help page and WSDL shows up fine. If not fix that issue first.
(3) If you are looking a default https endpoint after hosting it in IIS, make sure your IIS has https binding configured with an SSL certificate.
Hope this helps!
Thanks!

WCF Deploy Strange Contract behavior

WCF service deployed to IIS7 server sitting on WinServer2008 Standard SP2.
DataContract int Members are being 'lost'
i.e. All ints sent by the client emerge as 0 into the Contract processing logic on the Server
I made a 'Reflection' contract (Take incoming int turn it to string and return it). When the test harness is pointed at the WCF Contract running in the IDE the ints are reflected OK. When it is pointed at the Deployed contract zeros are reflected.
Deployment:
Made an IIS Application on the Server and did a file deployment to the physical directory.
The Deployed Service appears OK. i.e. The 'real' client app (VS2008 WinMobile 6.5 app under development separate solution) has a WebService reference that sees the Deployed WCF OK . It is just that the variable values generated by the client get lost on the wire. Only happens to Client generated variables. The client is able to consume Server generated DataContract Variables with the expected values. It is something to do with base types. The first victim was client generated dates so I moved these into strings for the journey.
Web.config is below.
thanks
Bob
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.serviceModel>
<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="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Removed the web Application from Production server and remade it. Problem partially solved. Test windows app now sees the ints reflected. However the WinMobile client still doesn't. Tested using string values for the contract and it works. Life is too short for this. I shall alter the Contracts to strings strings strings. So much for strongly typed Data Contracts.

Running WCF Service on Windows Web Server 2008 R2

I have a WCF service application with most basic settings working properly on my local computer. I had the error below when i deploy it on my test application server;
Module IIS Web Core
Notification Unknown
Handler Not yet determined
Error Code 0x80070032
Config Error The configuration section 'system.serviceModel' cannot be read because it is missing a section declaration
When i tried to deploy it test db server it worked properly.
After all searching i ended up with the problem on my test application server is that it has no Application Server role.
It seems there is no Application Server Role on Window Web Server 2008 R2 operation system.
So is there anyway / workaround to make my service work on Window Web Server 2008 R2 ?
Thanks.
Edit: I have two test machines. One for database (Windows Server 2008 R2 Standard) one for applications (Windows Web Server 2008 R2)
My web.config file content is below;
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<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="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
As the error indicates, there is a section missing in web.config.
What OS is the test machine running?
If you're hosting the service in IIS and have copied web.config from the test machine, you might need to add a System.WebServer section (this is a requirement of IIS 7).
If this doesn't help, please post your web.config. Also there might be more detailed information in the event viewer.
Sorry, but i had to answer my own question.
Short and simple; if you use WCF, do not use Windows Web Server 2008 R2. You will need the application server role, and Web Server 2008 R2 has no application server role. You can find further details on the net about this issue.(WCF Service Issue on Windows Server 2008 R2)

MvcMiniProfiler profiling web app and lower layers

I have MiniProfiler set up and working in my ASP.NET MVC app. My controllers make calls via WCF to a BLL which in turn talks to the database. I would like to see profiling from the WCF service alongside the existing profiling I see from the web app. Is it a case of making MiniProfiler a parameter in all service calls?
In a recent release of the MvcMiniProfiler they added WCF support (version 1.8 or greater). This is a 3 step process to get this working:
Add References
First add references to the MvcMiniprofiler and MvcMiniProfiler.WCF in your UI layer and WCF layer via nuget (or download the source and compile your own).
Setup WCF Host
Second, within the web.config of the service host you have to add the miniprofiler as an endpoint behavior. All of the config sections belong in "configuration/system.serviceModel".
<endpointBehaviors>
<behavior name="miniProfilerBehavior">
<wcfMiniProfilerBehavior />
</behavior>
</endpointBehaviors>
Then add the behavior extension (Note the version number needs to match your version of the MvcMiniProfiler.WCF):
<extensions>
<behaviorExtensions>
<add name="wcfMiniProfilerBehavior" type="MvcMiniProfiler.Wcf.WcfMiniProfilerBehavior, MvcMiniProfiler.Wcf, Version=1.8.0.0, Culture=neutral" />
</behaviorExtensions>
</extensions>
Then setup the endpoints to use the profiler behavior you setup:
<services>
<service behaviorConfiguration="BaseBehavior" name="BSI.Something">
<endpoint address="" behaviorConfiguration="miniProfilerBehavior" binding="basicHttpBinding" bindingConfiguration="http" contract="BSI.ISomething"/>
</service>
</services>
Depends on your setup but I had to add one more web.config setting to run all managed modules for all requests. This config is in the root "configuration" section:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
Setup WCF Client
Last, setup the wcf client to "turn on" the mvc profiler by doing much the same above.
Add the extension:
<extensions>
<behaviorExtensions>
<add name="wcfMiniProfilerBehavior" type="MvcMiniProfiler.Wcf.WcfMiniProfilerBehavior, MvcMiniProfiler.Wcf, Version=1.8.0.0, Culture=neutral" />
</behaviorExtensions>
</extensions>
Add a behavior:
<behaviors>
<endpointBehaviors>
<behavior name="wcfMiniProfilerBehavior">
<wcfMiniProfilerBehavior />
</behavior>
</endpointBehaviors>
</behaviors>
Setup the endpoints to use that behavior:
<client>
<endpoint address="http://something/Something.svc" behaviorConfiguration="wcfMiniProfilerBehavior"
binding="BasicHttpBinding" bindingConfiguration="BasicHttpBinding_HTTP"
contract="BSL.ISomething" name="BasicHttpBinding_ISomething" />
</client>
And you're done!
Side Note:
How does the MvcMiniProfiler actually work over WCF?
Basically the client behavior sets up a SOAP header that tells the wcf host to turn on the profiler. It passes that header along which is read by the endpoint behavior on the WCF host side. It then turns the profiler on in the host. Lastly when the WCF host is replying back to the client it stuffs all the profiler goodness into the SOAP response header which is in turn read by the WCF client. Pretty ingenious.
That's one method, but in order to get the reference to the libraries you would have to add references in the lower layers for MvcMiniProfiler anyway.
What I did in this very same situation is to take advantage of the global access point that MiniProfiler provides as a singleton. So, I just added the reference in the lower levels (deleted the stuff relative to MVC, such as the views) and just used MiniProfiler.Current as if I were on the upper layers.
It works like a charm. :)