Server.MapPath
How do I use % Feature X% from ASP.NET in a WCF service?
ASP.NET has an HTTP-centric application model with many great features specialized for web development in IIS. WCF is transport and host agnostic. Out of the box, WCF supports most of the features of ASP.NET in a way that makes sense independent of HTTP and IIS. Some of the features excluded from that set are supported as part of the specific implementation for the HTTP transport.
If you're porting an application from ASP.NET to WCF, you may find that one of the ASP.NET features not in the regularly supported subset has been made integral to the design of your application. WCF has an ASP.NET compatibility mode that gives you both the features and limitations of that environment. Using compatibility mode gets you your favorite ASP.NET %Feature X%:
- Session state
System.Web authorization, impersonation, and globalization
- IHttpModule
- AppSettings
- HttpContext
- MapPath
but the preference should be to use host and transport agnostic equivalents except when trying to get code working that already use these ASP.NET features.
This article was originally just about Server.MapPath, but I changed it because advice for the other features would read exactly the same.
Next time: Flow Throttles
Comments
Anonymous
October 05, 2007
"but the preference should be to use host and transport agnostic equivalents "...I don't fully understand this. If you're developing a service in the real world that is only ever going to be hosted in IIS, why add unnecessary complexity by jumping through hoops to cater for imaginary scenarios? And if this is being done for a specific client for instance, why waste their time and money in order to accommodate a requirement that they don't have?Anonymous
October 05, 2007
I want my service to have a set of endpoints that are conditionally enabled. Is it possible to put allAnonymous
October 05, 2007
Kevin, In most cases the agnostic feature is as easy or easier to use. We don't want you to have to spend more time coding to get these benefits. It's only in limited circumstances, many of which were mentioned in the article, that there's a difference. Even then, it's often only certain aspects of those features. For example, a lot of the things that HttpContext is used for can be done through OperationContext.Anonymous
October 06, 2007
The one thing I have wanted to see is how do you expose a simple asmx service which uses Windows Authentication and uses <authorization> block in web.config to limit which users (should be externally configurable) can access the web service. If I want to migrate this to WCF, how do you go about doing this in WCF with IIS Hosting. I have scoured the net and in all the forums I asked everybody seems to point to some MSDN doc which usually shows WCF service hosted in a console application, but never seem to answer my question (WCF + IIS + Windows Authentication + users externally configurable).Anonymous
October 08, 2007
Marc, Are you trying to do this with ASP.NET compat mode or not? It should work with no additional setup in compatibility mode.Anonymous
October 08, 2007
No I have not. I will try this. In that case should I enable Ingegrated Windows authentication on the IIS Virtual Directory? My current setup is Anonymous authentication on the Virtual Directory and am using "TransportCredentialOnly" security mode with Windows "ClientCredentials" for Services. It works but I have to programmatically check if the client is authorized or not. In asmx I could have just used the <authorization> element in the webconfig file. The most important thing for me is to have the authorized users be configurable externally rather than checking them programmatically.Anonymous
October 08, 2007
Marc, Yes, you need to set the vdir to Windows auth only. This article should cover the process step by step: http://www.rickgaribay.net/archive/2007/04/04/recipe-wcf-basichttpbinding-with-windows-authentication.aspx.Anonymous
October 09, 2007
Hi Nicholas, I followed everything laid out in the article. When I turn on Windows Integrated authentication and I browse the .svc file I get the following message Security settings for this service require 'Anonymous' Authentication but it is not enabled for the IIS application that hosts this service. Here is part of my web.config file <system.serviceModel> <bindings> <basicHttpBinding> <binding name="KBinding"> <security mode="TransportCredentialOnly"> <transport clientCredentialType="Windows" /> </security> </binding> </basicHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="MyServices.Calculator2Behavior"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="MyServices.Calculator2Behavior" name="MyServices.Calculator2"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="KBinding" contract="MyServices.ICalculator2" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> </system.serviceModel> I have also added the following line to web.config file <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> and turned on the AspNetCompatibilityRequirements attribute on the server. Still no luck. Does anything stand out that I am doing this incorrectly? Thanks for your help.