WCF没有在vb中添加服务引用

时间:2012-03-28 15:56:16

标签: vb.net wcf

我只是Silverlight和WCF的首发。我找到了Miguel A. Castro撰写的一篇非常好的文章here,该文章教授手动添加WCF。我有合同服务设置,我唯一剩下的就是从银行服务中获取数据。我很难将代码翻译成vb.net。

                BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
                EndpointAddress endpointAddress = new EndpointAddress("/Person.svc");
                IPersonService personService = new ChannelFactory<IPersonService>(basicHttpBinding, endpointAddress).CreateChannel();
                //+
                AsyncCallback asyncCallBack = delegate(IAsyncResult result)
                {
                    Person person = ((IPersonService)result.AsyncState).EndGetPersonData(result);
                    this.Dispatcher.BeginInvoke(delegate
                    {
                        spMain.Children.Add(new TextBlock
                        {
                            Text = person.FirstName 
                        });

                    });
                };
                personService.BeginGetPersonData("F488D20B-FC27-4631-9FB9-83AF616AB5A6", asyncCallBack, personService);

AsyncCallback asyncCallBack = delegate(IAsyncResult result) {

this.Dispatcher.BeginInvoke(delegate {

如何在vb.net中编写?

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

请找到转换为Vb.NET的C#代码

Dim basicHttpBinding As New BasicHttpBinding()
Dim endpointAddress As New EndpointAddress("/Person.svc")
Dim personService As IPersonService = New ChannelFactory(Of IPersonService)(basicHttpBinding, endpointAddress).CreateChannel()
'+
Dim asyncCallBack As AsyncCallback = Function(result As IAsyncResult) Do
    Dim person As Person = DirectCast(result.AsyncState, IPersonService).EndGetPersonData(result)
    Me.Dispatcher.BeginInvoke(Function() Do

        spMain.Children.Add(New TextBlock() With { _
            Key .Text = person.FirstName _
        })
    End Function)
End Function
personService.BeginGetPersonData("F488D20B-FC27-4631-9FB9-83AF616AB5A6", asyncCallBack, personService)

答案 1 :(得分:0)

您可以将代码分解为单独的subs,执行以下操作:

Dim asyncCallBack As AsyncCallback = AddressOf MyCallBack

Public Sub MyCallBack(ByVal Result As IAsyncResult)

    Me.Dispatcher.BeginInvoke(Sub() AddTextBlock())

End Sub

Public Sub AddTextBlock()

    'code to add new textblock here

End Sub

或查看Lambda Expressions

Dim asyncCallBack As AsyncCallback = Sub(result As IAsyncResult)

                                     End Sub

Me.Dispatcher.BeginInvoke(Sub()

                          End Sub)