Friday, September 03, 2010    
Blog  
OpenLight Blog
Dec 17

Written by: Ian T. Lackey
12/17/2009 1:38 AM 

Like many other developers I have been excited to see that WCF RIA Services has gone beta and now comes with a Go-Live license. After this release my team at Wash U began developing a few applications using the new RIA Services bits. We have been very impressed with the productivity increase as well as several other features that RIA provides over the previous straight up WCF services have offered in the past.

All was going well, until it came time to publish the first application to the staging (user testing) server. Upon deployment the application started displaying the “Initializing Application” progress control for close to a minute.

image

After which the application would throw an error: “Load operation failed for query ‘GetUser’. Unable to connect to SQL Server Database.“

image

This was a very strange error since we had configured the application to use Windows authentication and where not connecting to the application’s database on the initial screen. While the application was running on our local development machines everything worked fine, the application fired up and displayed the currently logged in username in the upper right of the application. So we looked at the configuration again to see if there was anything that we missed. By default RIA is configured to use Forms authentication. (As a side note, I found this post by Brad Abrams concerning the decision on which authentication method was more common: Forms Auth\Windows Auth – what is more common? ) Given this, the default configuration looks like this (Please note this is trimmed to display only the configuration elements used by the Authentication Service):

<roleManager enabled="true"/>
<authentication mode="Forms">
  <forms name=".RiaWinAuthExample_ASPXAUTH" />
</authentication>
<profile>
  <properties>
    <add name="FriendlyName"/>
  </properties>
</profile>

To get RIA to use Windows authentication on the local developer workstations we simply had to change the authentication element to the following:

<authentication mode="Windows" />

After verifying that the configuration published to the server did in fact contain this change we went on a hunt to find out why this would not work outside of localhost. One of the first articles we found was Tim Heuer’s post Deploying your Silverlight and WCF RIA Services application. This post shed a lot of light on possible details that may have been overlooked. After double checking the items mentioned in Tim’s article we continued to have the issue. (BTW: You may also need to add the System.ComponentModel.DataAnnotations.dll assembly to the list of items to set to “Copy Local” in addition to the items listed in the bin or not to bin section of the article.)

After digging some more and not finding much help on <Insert your favorite search engine here>, I decided to do some ground up debugging. This lead to several hours of starting new RIA projects, changing something, deploying it to a server and zeroing in on the problem. Eventually I came up with the following list of issues that were causing the error.

  1. The System.ComponentModel.DataAnnotations.dll was not included in the bin folder on the server.
    • This was fixed by setting the Copy Local property of the reference to true.
  2. We had neglected to enable impersonation in the configuration for the site. This was simply a silly mistake as we do this for all of our internal web applications.
    • Love it or hate it, we are pretty much required to use impersonation due to auditing requirements we have because of HIPAA regulations. I realize that there are other ways; however, this is the most straightforward way for us to be in compliance today. Going forward we are looking into possibly implementing other solutions.
  3. There are some additional configuration changes that are required when deploying to a non-developer workstation.
    • Specify the defaultProvider for the roleManager as: AspNetWindowsTokenRoleProvider
    • Set the enabled attribute on the profile element to false!
      • If you are using profiles the instead of setting this to false, the provider needs to be configured to use the proper database. If you need more information about profile providers, this article should get you started: ASP.NET Profile Providers

I realize that some of this may be the result of trying to rush the application out for demo; however, with the exception of Tim’s article there was not much on the web about deployment. Information about deploying using windows authentication is even more scarce. My hope is that this article will document all of the information in one place to save others the trouble of piecing this together for the many source I had to use.

Summary \ Solution

  • Check out Tim’s article to cover the RIA deployment bases.
  • Make sure IIS is properly configured to use Windows authentication. This is the same configuration for any other ASP.Net application that uses Windows authentication, nothing special here.
  • Ensure that System.ComponentModel.DataAnnotations.dll and all other referenced RIA assemblies are deployed to the bin folder on the server or RIA is installed on the server.
  • If you need impersonation, make sure it is specified in the configuration! (man, this one make me feel sloppy in my case)
  • Set the corresponding configuration elements as follows:
    • Again, this is trimmed to only show the sections the authentication service is concerned with, and the impersonation configuration is included. You may or may not need the identity element for your application. Like IIS impersonation settings are the same as any ASP.Net site, so follow the same rules regarding it.
<identity impersonate="true"/>
<authentication mode="Windows" />
 
<roleManager enabled="true" 
             defaultProvider="AspNetWindowsTokenRoleProvider"/>
<profile enabled="false" />

 

To summarize, the issues we encountered were related to:

  • ASP.Net’s default configuration attempting to use a local SQL Express database for membership, roles and providers. This was solved by using the proper configuration. For us, that meant disabling profiles altogether and changing the default role provider.
  • RIA assemblies are not deployed with the site by default and needed to be configured to be copied to the bin folder of the site or the RIA bits needed to be installed on the server.
  • Impersonation needed to be set to allow access to server side resources. (database connections, file system resources etc.)
    • I cannot stress enough that this may not apply to your application and needs to be considered based on your environment before enabling it. There are security philosophies that say this is an “evil” configuration.

I sure hope this saves other RIA developers some time and hair loss. Due to the voodoo that is RIA services, this was not necessarily an easy issue to figure out. I had to use Fiddler, Reflector and read several articles before it finally became clear. Once all of the pieces were exposed to the light it all made since, but brining it out of the “black magic” proved to be more challenging that it “should” have been. I guess that is the trade off for having such an easy development “story.”

Either way, I must say I am really digging the power of RIA and I have to recommend it to anyone want to build stunning LOB applications. So if you haven’t already, dig in and be amazed.

Happy Coding… err… Designing … umm … RIA Using.

As always, feedback is encouraged and appreciated!

Tags:

15 comment(s) so far...

Re: RIA Services, Windows Authentication & GetUser Error

Thanks Ian for this important post.
We also had to make all System.Web.* dlls the Copy Local to make it work.

By Alex Y on   12/18/2009 10:23 AM

Re: RIA Services, Windows Authentication & GetUser Error

Thanks Alex! It is interesting that you need to copy the System.Web.* dlls. These should all already exist in the server's GAC. Can you please provide a list of the dlls that were missing?

Thanks again for your feedback and input!

By Ian T. Lackey on   12/18/2009 10:24 AM

Re: RIA Services, Windows Authentication & GetUser Error

Hello Ian

Great Post.

I did the changes in the web.config as described by you as I am also using Windows Authentication in WCF ria services. I am getting the object refrence error

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.NullReferenceException: Object reference not set to an instance of an object.
at System.ServiceModel.Description.WsdlExporter.WSPolicyAttachmentHelper.InsertPolicy(String key, ServiceDescription policyWsdl, ICollection`1 assertions)
at System.ServiceModel.Description.WsdlExporter.WSPolicyAttachmentHelper.AttachPolicy(ServiceEndpoint endpoint, WsdlEndpointConversionContext endpointContext, PolicyConversionContext policyContext)
at System.ServiceModel.Description.WsdlExporter.ExportEndpoint(ServiceEndpoint endpoint, XmlQualifiedName wsdlServiceQName)
at System.ServiceModel.Description.WsdlExporter.ExportEndpoints(IEnumerable`1 endpoints, XmlQualifiedName wsdlServiceQName)
at System.ServiceModel.Description.ServiceMetadataBehavior.MetadataExtensionInitializer.GenerateMetadata()
at System.ServiceModel.Description.ServiceMetadataExtension.EnsureInitialized()
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.InitializationData.InitializeFrom(ServiceMetadataExtension extension)
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.GetInitData()
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.TryHandleDocumentationRequest(Message httpGetRequest, String[] queries, Message& replyMessage)
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.ProcessHttpRequest(Message httpGetRequest)
at System.ServiceModel.Description.ServiceMetadataExtension.HttpGetImpl.Get(Message message)
at SyncInvokeGet(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

By Goldy on   12/31/2009 1:38 AM

Re: RIA Services, Windows Authentication & GetUser Error

Goldy,
I did a quick search on the error you posted and it appears to be linked to Silverlight 4 and/or IIS 7+. So far I do not see a fix or workaround for it, but here are a few threads to keep an eye on. It appears Microsoft is looking into the issue. The post was based on the Silverlight 3 WCF RIA services and as far as I can tell this is not an issue on that platform.
forums.silverlight.net/forums/t/148476.aspx
connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=520388&wa=wsignin1.0
forums.silverlight.net/forums/p/150037/334579.aspx

By Ian T. Lackey on   12/31/2009 1:40 AM

Re: RIA Services, Windows Authentication & GetUser Error

Great! The articles so far have been full of great material. Glad to hear the serious will continue. Keep 'em coming!

By Joy on   2/3/2010 11:31 PM

Re: RIA Services, Windows Authentication & GetUser Error

Thanks Joy I really apperciate that!

By Ian T. Lackey on   2/3/2010 11:32 PM

Re: RIA Services, Windows Authentication & GetUser Error

I'm still in dev mode with VS2010 Beta, SL4 beta and WCF RIA beta and was eventual receiving the error after a few seconds. I understand that a deployment solution is not yet available, but was wondering if there were dev mode settings I should be setting to prevent this from happening in this mode? I modified the config.sys and made sure the RIA bits and System.Web.* are copied local per this thread; this seemed to solve my immediate issues. Thanks.

By Marc W. George on   2/24/2010 11:07 AM

Re: RIA Services, Windows Authentication & GetUser Error

I've just posted my deploy configuration (Silverlight and RIAService IIS7 SQLSERVER 2008 OS Vista) on
forum.aspitalia.com/forum/post/357219/Pubblicazione-Wcf-Ria-Services.aspx?PageIndex=2

I hope it can helping someone to deploying your software!

dario

By dario.culoma on   2/24/2010 11:07 AM

Re: RIA Services, Windows Authentication & GetUser Error

-Marc: Unfortunately I have not seen any official recommendations for deployment either. At this time, I can only recommend what has been stated in this article. I am unclear of what you mean by "dev mode" other than possible changes to connection string and debug option in the web.config

-Dario: Thanks for sharing!

By Ian T. Lackey on   2/24/2010 11:09 AM

Re: RIA Services, Windows Authentication & GetUser Error

I've been pulling my hair out trying to deploy just the Business Application Template. I get:

Attempt by method 'System.ServiceModel.DomainServices.Hosting.PoxBinaryEndpointFactory.CreateEndpoints(System.ServiceModel.DomainServices.Server.DomainServiceDescription, System.ServiceModel.DomainServices.Hosting.DomainServiceHost)' to access method 'System.ServiceModel.Description.ServiceMetadataContractBehavior..ctor()' failed.

Any ideas?

By Tom on   4/23/2010 9:31 AM

Re: RIA Services, Windows Authentication & GetUser Error

According to this it requires Full Trust: msdn.microsoft.com/en-us/library/system.servicemodel.domainservices.hosting.poxbinaryendpointfactory.createendpoints(VS.91).aspx are you attempting to run under medium trust?

By Ian T. Lackey on   4/23/2010 9:31 AM

Re: RIA Services, Windows Authentication & GetUser Error

Thanks, You saved my hair -))) and time

By Vitaliy on   5/22/2010 3:22 PM

Re: RIA Services, Windows Authentication & GetUser Error

Thanks Vitaliy, I am glad it helped... and trust me I know what you mean. I lost much hair and time trying to figure this one out. :P

BTW: Everyone, the better solution is upgrade to Silverlight 4. ;)

By Ian T. Lackey on   5/22/2010 3:26 PM

Re: RIA Services, Windows Authentication & GetUser Error

The key point when using windows authentication is to Disable profile by setting as Ian mentioned above.

By SKEF on   8/15/2010 6:27 PM

Re: RIA Services, Windows Authentication & GetUser Error

I banged my head on the wall for 2 days, then I ran into this ... THANK YOU SO MUCH, i can breathe again !!!!!!

By d on   8/15/2010 6:31 PM

Your name:
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
  
Copyright 2009 by OpenLightGroup.net   |  Privacy Statement  |  Terms Of Use