[WCF .NET] Overloaded functies gebruiken

Pagina: 1
Acties:
  • 116 views sinds 30-01-2008
  • Reageer

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Hallo allemaal,

Ik heb een interface waarin mijn functies gedeclareert zijn. Een aantal overloaden elkaar en hebben dus dezelfde naam. WCF kan hiermee niet overweg en er wordt gezegd dat ik de Name property binnen de OperationalContractAttribute moet veranderen om de naamgeving van die functie te veranderen. Dit heb ik ook toegepast. Toch krijg ik een exception. De code is hieronder weergegeven. De bijbehorende exception is helemaal onderaan weergegeven. Iemand een idee waar het precies fout gaat? Alvast bedankt.


De ISynopServiceContract Interface:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Imports System.ServiceModel

Public Interface ISynopServiceContract

    <OperationContractAttribute(Name:="GetDataByYear")> _
    Function GetData(ByVal varName As String, ByVal year As Integer) As Synop

    <OperationContractAttribute(Name:="GetDataByYearMonth")> _
    Function GetData(ByVal varName As String, ByVal year As Integer, ByVal month As Integer) As Synop

    <OperationContractAttribute(Name:="GetDataByDateRange")> _
    Function GetData(ByVal varName As String, ByVal startDateTime As DateTime, ByVal endDateTime As DateTime) As Synop

    <OperationContractAttribute(Name:="ExistsOfYear")> _
    Function ExistsSynop(ByVal varName As String, ByVal year As Integer) As Boolean

    <OperationContractAttribute(Name:="ExistsOfYearMonth")> _
    Function ExistsSynop(ByVal varName As String, ByVal year As Integer, ByVal month As Integer) As Boolean

    ' Not a operational contract.
    Function Save(ByVal data As Synop) As Boolean

End Interface


De SynopService Class:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Imports System.ServiceModel

Public Class SynopService
    Implements ISynopServiceContract

    Private mManager As SynopManager
    Public Sub New()
        Try
            mManager = New SynopManager()
        Catch ex As Exception
            Throw New Exception(ex.Message, ex)
        End Try
    End Sub


    Public Function ExistsSynop(ByVal varName As String, ByVal year As Integer) As Boolean Implements ISynopServiceContract.ExistsSynop
        Return mManager.ExistsSynop(varName, year)
    End Function

    Public Function ExistsSynop(ByVal varName As String, ByVal year As Integer, ByVal month As Integer) As Boolean Implements ISynopServiceContract.ExistsSynop
        Return mManager.ExistsSynop(varName, year, month)
    End Function

    Public Function GetData(ByVal varName As String, ByVal startDateTime As Date, ByVal endDateTime As Date) As Synop Implements ISynopServiceContract.GetData
        Return mManager.GetData(varName, startDateTime, endDateTime)
    End Function

    Public Function GetData(ByVal varName As String, ByVal year As Integer) As Synop Implements ISynopServiceContract.GetData
        Return mManager.GetData(varName, year)
    End Function

    Public Function GetData(ByVal varName As String, ByVal year As Integer, ByVal month As Integer) As Synop Implements ISynopServiceContract.GetData
        Return mManager.GetData(varName, year, month)
    End Function

    Public Function Save(ByVal data As Synop) As Boolean Implements ISynopServiceContract.Save
        Return mManager.Save(data)
    End Function

End Class


De Exception
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[InvalidOperationException: Cannot have two operations in the same contract with the same name, methods GetData and GetData in type ServiceContracts.ISynopServiceContract violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.]
   System.ServiceModel.Description.TypeLoader.CreateOperationDescription(ContractDescription contractDescription, MethodInfo methodInfo, MessageDirection direction, ContractReflectionInfo reflectionInfo, ContractDescription declaringContract) +3287
   System.ServiceModel.Description.TypeLoader.CreateOperationDescriptions(ContractDescription contractDescription, ContractReflectionInfo reflectionInfo, Type contractToGetMethodsFrom, ContractDescription declaringContract, MessageDirection direction) +177
   System.ServiceModel.Description.TypeLoader.CreateContractDescription(ServiceContractAttribute contractAttr, Type contractType, Type serviceType, ContractReflectionInfo& reflectionInfo, Object serviceImplementation) +1038
   System.ServiceModel.Description.TypeLoader.LoadContractDescriptionHelper(Type contractType, Type serviceType, Object serviceImplementation) +204
   System.ServiceModel.Description.ContractDescription.GetContract(Type contractType, Type serviceType) +53
   System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2& implementedContracts) +470
   System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) +145
   System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) +258
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses) +29
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +307
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath) +702
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +32
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +430

[ServiceActivationException: The service '/Synop/SynopService.svc' cannot be activated due to an exception during compilation.  The exception message is: Cannot have two operations in the same contract with the same name, methods GetData and GetData in type ServiceContracts.ISynopServiceContract violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute..]
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +909
   System.ServiceModel.ServiceHostingEnvironment.EnsureServiceAvailableFast(String relativeVirtualPath) +356
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest() +234
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest() +727
   System.ServiceModel.Activation.HostedHttpModuleRequestAsyncResult.BeginRequest() +141
   System.ServiceModel.Activation.HttpModule.StartBeginProcessRequest(Object sender, EventArgs e, AsyncCallback cb, Object extraData) +68
   System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +148
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +138
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +65

Acties:
  • 0 Henk 'm!

  • riezebosch
  • Registratie: Oktober 2001
  • Laatst online: 10-09 11:15
Wat heb je in je .svc staan?

Canon EOS 400D + 18-55mm F3.5-5.6 + 50mm F1.8 II + 24-105 F4L + 430EX Speedlite + Crumpler Pretty Boy Back Pack


Acties:
  • 0 Henk 'm!

  • whoami
  • Registratie: December 2000
  • Laatst online: 23:30
Moet je die attributes ook niet op de methods in je implementatie gaan toevoegen ?

https://fgheysels.github.io/


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
De SynopService.svc
code:
1
<% @ServiceHost Language=VB Debug="true" Service="SynopService" CodeBehind="~/App_Code/SynopService.vb" %>

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
whoami schreef op woensdag 17 oktober 2007 @ 12:36:
Moet je die attributes ook niet op de methods in je implementatie gaan toevoegen ?
Heb ik al geprobeerd, maar dan krijg je andere fouten.... Dat is het niet denk ik. De interface bepaald hoe de functies door de applicaties aangeroepen kunnen worden. De implementatie SynopService implementeert de functies van de Interface en daarin is zeg maar de functionaliteit verwerkt.

Acties:
  • 0 Henk 'm!

  • Niemand_Anders
  • Registratie: Juli 2006
  • Laatst online: 09-07-2024

Niemand_Anders

Dat was ik niet..

De 'Name' property van OperationalContractAttribute werkt alleen als alias. WFC zelf kan niet overweg met overloaded functies. De Name property wordt over het algemeen gebruikt als de naam een geregistreerd keyword is. De werking van dit attribute is redelijk gelijk aan DllImport.

Zelf maak ik gebruik van een wrapper class welke intern de daadwerkelijke functie aanroept:
C#:
1
2
3
4
5
6
7
8
9
public class SynopServiceWrapper : ISynopServiceContract
{
  private SynopService svc = new SynopService(); 

  public Synop GetDataByYear(string varName, int year)
  {
    return svc.GetData(varName, year);
  }
}


Dit werkt gewoon goed zonder andere rare kunstgrepen. WFC heeft momenteel geen support voor overloaded functions. De WFC Proxy snapt dat nog niet. De reden hiervoor is namelijk dat niet alle talen overloaded functions ondersteunen. De WFC Proxy is CLR compatible, wat niet automatisch inhoud dat alle features van een taal ondersteund worden.

If it isn't broken, fix it until it is..


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
De oplossing met de Wrapper Class heb ik al eerder ingebouwd, alleen vond ik dit wel overbodig, want ik las dat doormiddel van <OperationContract(Name:="de naam")> ik dit kon verwerken...

Hmm... Dat is wel raar, maar waarom wordt door MSDN verteld dat je de naamgevingen doormiddel van OperationContract(Name:="") kan scheiden?

Ik zal anders wel de wrapper class gebruiken. Maar toch ben ik benieuwd of er andere mogelijkheden zijn. Iemand anders een idee?

Niemand_Anders, bedankt iig voor je reactie ;)

Waarom gebruik je trouwens
code:
1
public class SynopServiceWrapper : ISynopServiceContract


je bedoeld zeker:
code:
1
public class SynopServiceWrapper


of niet?

[ Voor 14% gewijzigd door Verwijderd op 17-10-2007 14:17 ]


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Ook wanneer ik de wrapper class gebruik, gaat het nog steeds fout.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[InvalidOperationException: Cannot have two operations in the same contract with the same name, methods GetData and GetData in type ServiceContracts.ISynopServiceContract violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute.]
   System.ServiceModel.Description.TypeLoader.CreateOperationDescription(ContractDescription contractDescription, MethodInfo methodInfo, MessageDirection direction, ContractReflectionInfo reflectionInfo, ContractDescription declaringContract) +3287
   System.ServiceModel.Description.TypeLoader.CreateOperationDescriptions(ContractDescription contractDescription, ContractReflectionInfo reflectionInfo, Type contractToGetMethodsFrom, ContractDescription declaringContract, MessageDirection direction) +177
   System.ServiceModel.Description.TypeLoader.CreateContractDescription(ServiceContractAttribute contractAttr, Type contractType, Type serviceType, ContractReflectionInfo& reflectionInfo, Object serviceImplementation) +1038
   System.ServiceModel.Description.TypeLoader.LoadContractDescriptionHelper(Type contractType, Type serviceType, Object serviceImplementation) +204
   System.ServiceModel.Description.ContractDescription.GetContract(Type contractType, Type serviceType) +53
   System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2& implementedContracts) +470
   System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) +145
   System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) +258
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses) +29
   System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +307
   System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath) +702
   System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +32
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +430

[ServiceActivationException: The service '/Synop/SynopService.svc' cannot be activated due to an exception during compilation.  The exception message is: Cannot have two operations in the same contract with the same name, methods GetData and GetData in type ServiceContracts.ISynopServiceContract violate this rule. You can change the name of one of the operations by changing the method name or by using the Name property of OperationContractAttribute..]
   System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +909
   System.ServiceModel.ServiceHostingEnvironment.EnsureServiceAvailableFast(String relativeVirtualPath) +356
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest() +234
   System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest() +727
   System.ServiceModel.Activation.HostedHttpModuleRequestAsyncResult.BeginRequest() +141
   System.ServiceModel.Activation.HttpModule.StartBeginProcessRequest(Object sender, EventArgs e, AsyncCallback cb, Object extraData) +68
   System.ServiceModel.Activation.HttpModule.ProcessRequest(Object sender, EventArgs e) +148
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +138
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +65

Acties:
  • 0 Henk 'm!

  • riezebosch
  • Registratie: Oktober 2001
  • Laatst online: 10-09 11:15
Verwijderd schreef op woensdag 17 oktober 2007 @ 13:51:
[...]
Waarom gebruik je trouwens
code:
1
public class SynopServiceWrapper : ISynopServiceContract


je bedoeld zeker:
code:
1
public class SynopServiceWrapper


of niet?
Nee, hij gebruikt C# ;)

Canon EOS 400D + 18-55mm F3.5-5.6 + 50mm F1.8 II + 24-105 F4L + 430EX Speedlite + Crumpler Pretty Boy Back Pack

Pagina: 1