Solved

Urgent WCF DATA CONTRACT

Posted on 2008-10-13
5
3,786 Views
Last Modified: 2013-11-26
I am trying to implement a WCF Webservice and I have created a datacontract, which code is as follows on my client I am trying to call the operation contract GetUser but I get that error.

I tried to pass a dataset, a string all the common types were returned sucessfully but not with MembershipUser or WCFMembershipUser.

Can you see where I havent done what I should?
Imports System.Web

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports System.Collections.Generic

Imports System.DirectoryServices

Imports System.Data

 

Public Class membershipprovider

    Implements IMembershipProvider

 

 

    Public Sub New()

 

    End Sub

 

    Protected Function GetProvider(ByVal providerName As String) As System.Web.Security.MembershipProvider

        Return System.Web.Security.Membership.Providers("MyADMembershipProvider")

    End Function

 

    Protected Function ConvertUsers(ByVal user As System.Web.Security.MembershipUser) As WCFMembershipUser

        If user Is Nothing Then

            Return Nothing

        End If

 

        Dim membershipUser As WCFMembershipUser = New WCFMembershipUser(Now, user.Comment, user.CreationDate, user.Email, _

                                                                        user.IsApproved, user.IsLockedOut, user.LastLockoutDate, _

                                                                        user.ProviderUserKey, user.UserName, Now, True)

 

        Return membershipUser

    End Function

 

    Protected Function ConvertUser(ByVal provider As System.Web.Security.MembershipProvider, ByVal user As MembershipUser) As System.Web.Security.MembershipUser

        If user Is Nothing Then

            Return Nothing

        End If

 

        Dim membershipUser As New System.Web.Security.MembershipUser(provider.Name, _

            user.UserName, _

            user.ProviderUserKey, _

            user.Email, _

            user.PasswordQuestion, _

            user.Comment, _

            user.IsApproved, _

            user.IsLockedOut, _

            user.CreationDate, _

            user.LastLoginDate, _

            user.LastActivityDate, _

            user.LastPasswordChangedDate, _

            user.LastLockoutDate)

 

        Return membershipUser

    End Function

 

    Protected Function BuildUserList(ByVal collection As System.Web.Security.MembershipUserCollection) As List(Of WCFMembershipUser)

        If collection Is Nothing Then

            Return Nothing

        End If

        Dim list As New List(Of WCFMembershipUser)

        Dim user As System.Web.Security.MembershipUser

        For Each user In collection

            list.Add(ConvertUsers(user))

        Next

        Return list

    End Function

 

    Public Function ChangePassword(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal oldPassword As String, ByVal newPassword As String) As Boolean Implements IMembershipProvider.ChangePassword

        Return GetProvider(providerName).ChangePassword(username, oldPassword, newPassword)

    End Function

 

    Public Function ChangePasswordQuestionAndAnswer(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal password As String, ByVal newPasswordQuestion As String, ByVal newPasswordAnswer As String) As Boolean Implements IMembershipProvider.ChangePasswordQuestionAndAnswer

        Return GetProvider(providerName).ChangePasswordQuestionAndAnswer(username, password, newPasswordQuestion, newPasswordAnswer)

    End Function

 

    Public Function DeleteUser(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal deleteAllRelatedData As Boolean) As Boolean Implements IMembershipProvider.DeleteUser

        Return GetProvider(providerName).DeleteUser(username, deleteAllRelatedData)

    End Function

 

    Public Function FindUsersByName(ByVal providerName As String, ByVal applicationName As String, ByVal usernameToMatch As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Collections.Generic.List(Of WCFMembershipUser) Implements IMembershipProvider.FindUsersByName

        Return BuildUserList(GetProvider(providerName).FindUsersByName(usernameToMatch, pageIndex, pageSize, totalRecords))

    End Function

 

    Public Function GetAllUsers(ByVal providerName As String, ByVal applicationName As String, ByVal pageIndex As Integer, ByVal pageSize As Integer, ByRef totalRecords As Integer) As System.Collections.Generic.List(Of WCFMembershipUser) Implements IMembershipProvider.GetAllUsers

        Return BuildUserList(GetProvider(providerName).GetAllUsers(pageIndex, pageSize, totalRecords))

    End Function

 

    Public Function GetNumberOfUsersOnline(ByVal providerName As String, ByVal applicationName As String) As Integer Implements IMembershipProvider.GetNumberOfUsersOnline

        Return GetProvider(providerName).GetNumberOfUsersOnline()

    End Function

 

 

    Public Function GetPassword(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal answer As String) As String Implements IMembershipProvider.GetPassword

        Return GetProvider(providerName).GetPassword(username, answer)

    End Function

 

 

    Public Function GetUserByUserKey(ByVal providerName As String, ByVal applicationName As String, ByVal providerUserKey As Object, ByVal userIsOnline As Boolean) As WCFMembershipUser Implements IMembershipProvider.GetUserByUserKey

        Return ConvertUsers(GetProvider(providerName).GetUser(providerName, userIsOnline))

    End Function

 

 

    Public Function GetUserByUsername(ByVal providerName As String, ByVal username As String) As MembershipUser Implements IMembershipProvider.GetUserByUsername

 

        Return Membership.GetUser(username)

    End Function

 

 

    Public Function ResetPassword(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal answer As String) As String Implements IMembershipProvider.ResetPassword

        Return GetProvider(providerName).ResetPassword(username, answer)

    End Function

 

 

    Public Function UnlockUser(ByVal providerName As String, ByVal applicationName As String, ByVal userName As String) As Boolean Implements IMembershipProvider.UnlockUser

        Return GetProvider(providerName).UnlockUser(userName)

    End Function

 

 

    Public Function UpdateUser(ByVal providerName As String, ByVal applicationName As String, ByVal user As MembershipUser) As Object Implements IMembershipProvider.UpdateUser

        Dim provider As Security.MembershipProvider = GetProvider(providerName)

        provider.UpdateUser(ConvertUser(provider, user))

    End Function

 

 

    Public Function ValidateUser(ByVal providerName As String, ByVal applicationName As String, ByVal username As String, ByVal password As String) As Boolean Implements IMembershipProvider.ValidateUser

        Dim provider As Security.MembershipProvider = GetProvider(providerName)

 

        Return provider.ValidateUser(username, password)

    End Function

 

    Public Function CreateUser(ByVal username As String, ByVal password As String) As WCFMembershipUser Implements IMembershipProvider.CreateUser

        Return ConvertUsers(Membership.CreateUser(username, password))

    End Function

End Class

 

 

 

-------------Server Config----------------------------------------------

  <system.serviceModel>

        <serviceHostingEnvironment aspNetCompatibilityEnabled="false"/>

        <services>

 

            <service behaviorConfiguration="Service1Behavior" name="RoleProvider">

                <endpoint address="" binding="wsHttpBinding" contract="IRoleProvider">

                    <identity>

                        <dns value="localhost"/>

                    </identity>

                </endpoint>

                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

            </service>

            <service behaviorConfiguration="MembershipProviderBehavior" name="membershipprovider">

                <endpoint address="" binding="wsHttpBinding" contract="IMembershipProvider">

                    <identity>

                        <dns value="localhost"/>

                    </identity>

                </endpoint>

                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

            </service>

            <service behaviorConfiguration="ActiveDirectoryManagerBehavior" name="ActiveDirectoryManager">

                <endpoint address="" binding="wsHttpBinding" contract="IActiveDirectoryManager">

                    <identity>

                        <dns value="localhost"/>

                    </identity>

                </endpoint>

                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

            </service>

        </services>

        <behaviors>

            <serviceBehaviors>

                <behavior name="Service1Behavior">

                    <serviceMetadata httpGetEnabled="true"/>

                    <serviceDebug includeExceptionDetailInFaults="true"  httpsHelpPageEnabled="true" httpHelpPageEnabled="true"/>

                    

                </behavior>

                <behavior name="MembershipProviderBehavior">

                    <serviceMetadata httpGetEnabled="true"/>

                    <serviceDebug includeExceptionDetailInFaults="true" httpsHelpPageEnabled="true" httpHelpPageEnabled="true"/>

                </behavior>

                <behavior name="ActiveDirectoryManagerBehavior">

                    <serviceMetadata httpGetEnabled="true"/>

                    <serviceDebug includeExceptionDetailInFaults="true" httpsHelpPageEnabled="true" httpHelpPageEnabled="true"/>

                </behavior>

            </serviceBehaviors>

        </behaviors>

    </system.serviceModel>

</configuration>

 

 

-------------------------Client Config---------------------------------

 <client>

            <endpoint address="http://localhost:4617/WCFAASecurityServices/MembershipProvider.svc"

               binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IMembershipProvider"

               contract="IMembershipProvider" name="WSHttpBinding_IMembershipProvider">

                <identity>

                    <dns value="localhost" />

                </identity>

            </endpoint>

            <endpoint address="http://localhost:4617/WCFAASecurityServices/RoleProvider.svc"

                 binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IRoleProvider"

                 contract="IRoleProvider" name="WSHttpBinding_IRoleProvider">

                <identity>

                    <dns value="localhost" />

                </identity>

            </endpoint>

            <endpoint address="http://localhost:4617/WCFAASecurityServices/ActiveDirectoryManager.svc"

                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IActiveDirectoryManager"

                contract="IActiveDirectoryManager" name="WSHttpBinding_IActiveDirectoryManager">

                <identity>

                    <dns value="localhost" />

                </identity>

            </endpoint>

        </client>

Open in new window

0
Comment
Question by:Juliafrazer
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:novynov
Comment Utility
Exactly what error are you getting? I didn't see it described above.
0
 

Author Comment

by:Juliafrazer
Comment Utility
The error I am getting is the following,

The underlying connection was closed: The connection was closed unexpectedly.

the cause is because I am trying to pass a type of MembershipUser or WCFMembershipUser (a datacontract that I ve created)  to the client, I tried to pass a String,a dataset and it works but not with a more complex type.
0
 

Author Comment

by:Juliafrazer
Comment Utility
at System.Net.HttpWebRequest.GetResponse()    at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)"
0
 
LVL 4

Expert Comment

by:novynov
Comment Utility
Not sitting in front of a debugger looking at this, I may be grasping, but...

At a high level, there are a few places where an exception may likely originate in a WCF call: By the proxy during message creation and send, in the call itself, and on the service side when preparing the response. I've seen #1 and #3 happen due to serialization issues.

That said, what you describe almost sounds like #2. Some kind of exception is getting thrown on the server during message processing, and then the connection is lost/reset. Basically, it looks like the exception you're seeing may be a side effect of another problem.

To get to the bottom of this, we need to ascertain what exactly is causing the exception. The exception/fault that you are seeing looks like one of those exceptions that is "wrapping" the real cause. This obfuscation can happen when debugging things like WCF services - depending on how you handle your exceptions on the server side.

So, I'd recommend two things as a next step to discovering the root cause:

- Inspect the inner exception of the exception you're seeing on the client. Is it something besides null? Does it reveal anything of value?
- Place a debugger against your WCF service (remember, the debugger needs to be attached to the process of the service, not the service calling client. Place a breakpoint on call enter, and step through the entire call. Does the message come in? Does an exception happen somewhere in the call? Does the exception happen after all of the message handling routine is complete?

Let me know what you find out.
0
 

Accepted Solution

by:
Juliafrazer earned 0 total points
Comment Utility
i think I found what it was, I took out some of the attributes of the WCFMembershipUser objects because they were having a Null value and somehow they were causing the problem. Now its working fine.

novynov thanks for your help.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
javascript call 6 34
Default parameter problem in C# 3 25
Trouble with References... 5 21
Problem to page 4 11
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now