Vb.net netpipebinding与codedom程序集客户端
(对不起我的英语),现在这是我第一次使用WCF,所以我的问题可以很简单)所以:有一个例子来自MSDN:1解决方案-2项目:服务和客户端,如下所示:服务:Vb.net netpipebinding与codedom程序集客户端,vb.net,wcf,codedom,Vb.net,Wcf,Codedom,(对不起我的英语),现在这是我第一次使用WCF,所以我的问题可以很简单)所以:有一个例子来自MSDN:1解决方案-2项目:服务和客户端,如下所示:服务: <ServiceContract()> _ Public Interface ICalculator <OperationContract()> _ Function Openserv(ByVal n1 As Integer) As Boolean End Interfac
<ServiceContract()> _
Public Interface ICalculator
<OperationContract()> _
Function Openserv(ByVal n1 As Integer) As Boolean
End Interface
Public Class Service
Implements ICalculator
Dim servicehost As System.ServiceModel.ServiceHost
Private Sub Service_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim baseAddress As Uri = New Uri("http://localhost:8000/ServiceModelSamples/service")
Dim address As String = "net.pipe://localhost/ServiceModelSamples/service"
servicehost = New System.ServiceModel.ServiceHost(GetType(Service), baseAddress)
Dim binding As System.ServiceModel.NetNamedPipeBinding = New System.ServiceModel.NetNamedPipeBinding(System.ServiceModel.NetNamedPipeSecurityMode.Transport)
binding.CloseTimeout = New TimeSpan(0, 1, 0)
binding.Name = "Binding1"
binding.OpenTimeout = New TimeSpan(0, 1, 0)
binding.ReceiveTimeout = New TimeSpan(0, 10, 0)
binding.SendTimeout = New TimeSpan(0, 10, 0)
binding.TransactionFlow = False
binding.TransactionProtocol = System.ServiceModel.TransactionProtocol.OleTransactions
binding.TransferMode = System.ServiceModel.TransferMode.Buffered
binding.HostNameComparisonMode = System.ServiceModel.HostNameComparisonMode.StrongWildcard
binding.MaxBufferPoolSize = 524288
binding.MaxBufferSize = 65536
binding.MaxConnections = 10
binding.MaxReceivedMessageSize = 65536
servicehost.AddServiceEndpoint(GetType(ICalculator), binding, address)
Dim smb As System.ServiceModel.Description.ServiceMetadataBehavior = New System.ServiceModel.Description.ServiceMetadataBehavior
smb.HttpGetEnabled = True
servicehost.Description.Behaviors.Add(smb)
servicehost.Open()
End Sub
Public Function Openserv(ByVal n1 As Integer) As Boolean _
Implements ICalculator.Openserv
If n1 > 0 Then
filename &= n1 & ":"
End If
Dim result As Boolean = True
Return result
End Function
End Class
_
公共接口计算器
_
函数Openserv(ByVal n1作为整数)作为布尔值
端接口
公务舱服务
电子计算器
Dim servicehost作为System.ServiceModel.servicehost
私有子服务_Load(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理MyBase.Load
作为Uri的Dim基地址=新Uri(“http://localhost:8000/ServiceModelSamples/service")
Dim地址为String=“net。pipe://localhost/ServiceModelSamples/service"
servicehost=New System.ServiceModel.servicehost(GetType(Service),baseAddress)
Dim绑定为System.ServiceModel.NetNamedPipeBinding=新的System.ServiceModel.NetNamedPipeBinding(System.ServiceModel.NetNamedPipeSecurityMode.Transport)
binding.CloseTimeout=新的时间跨度(0,1,0)
binding.Name=“Binding1”
binding.OpenTimeout=新的时间跨度(0,1,0)
binding.ReceiveTimeout=新的时间跨度(0,10,0)
binding.SendTimeout=新的时间跨度(0,10,0)
binding.TransactionFlow=False
binding.TransactionProtocol=System.ServiceModel.TransactionProtocol.OleTransactions
binding.TransferMode=System.ServiceModel.TransferMode.Buffered
binding.HostNameComparisonMode=System.ServiceModel.HostNameComparisonMode.strong通配符
binding.MaxBufferPoolSize=524288
binding.MaxBufferSize=65536
binding.MaxConnections=10
binding.MaxReceivedMessageSize=65536
AddServiceEndpoint(GetType(ICalculator)、绑定、地址)
Dim smb As System.ServiceModel.Description.ServiceMetadataBehavior=新System.ServiceModel.Description.ServiceMetadataBehavior
smb.HttpGetEnabled=True
servicehost.Description.Behaviors.Add(smb)
servicehost.Open()
端接头
作为布尔值的公共函数Openserv(ByVal n1为整数)_
实现ICalculator.Openserv
如果n1>0,则
文件名&=n1&“:”
如果结束
将结果设置为布尔值=真
返回结果
端函数
末级
客户:
<System.ServiceModel.ServiceContract()> _
Public Interface ICalculatorClient
<System.ServiceModel.OperationContract()> _
Function Openserv(ByVal n1 As Integer) As Boolean
End Interface
Partial Public Class CalculatorClient
Inherits System.ServiceModel.ClientBase(Of ICalculatorClient)
Implements ICalculatorClient
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal endpointConfigurationName As String)
MyBase.New(endpointConfigurationName)
End Sub
Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As String)
MyBase.New(endpointConfigurationName, remoteAddress)
End Sub
Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
MyBase.New(endpointConfigurationName, remoteAddress)
End Sub
Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)
MyBase.New(binding, remoteAddress)
End Sub
Public Function OpenServ(ByVal n1 As Integer) As Boolean Implements ICalculatorClient.Openserv
Return MyBase.Channel.Openserv(n1)
End Function
End Class
Public Class Client
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bld As New System.ServiceModel.NetNamedPipeBinding
Dim epa As New System.ServiceModel.EndpointAddress("net.pipe://localhost/servicemodelsamples/service")
Dim client As New CalculatorClient(bld, epa)
Dim value1 As Integer = 33
Dim result As Boolean = client.OpenServ(value1)
client.Close()
MessageBox.Show("Открыто в основном приложении")
End Sub
End Class
_
公共接口iCalculator客户端
_
函数Openserv(ByVal n1作为整数)作为布尔值
端接口
部分公共类计算器客户端
继承System.ServiceModel.ClientBase(ICalculatorClient的)
实现iCalculator客户端
公共分新()
MyBase.New()
端接头
Public Sub New(ByVal endpointConfigurationName作为字符串)
MyBase.New(endpointConfigurationName)
端接头
Public Sub New(ByVal endpointConfigurationName作为字符串,ByVal remoteAddress作为字符串)
MyBase.New(endpointConfigurationName,remoteAddress)
端接头
Public Sub New(ByVal endpointConfigurationName作为字符串,ByVal remoteAddress作为System.ServiceModel.EndpointAddress)
MyBase.New(endpointConfigurationName,remoteAddress)
端接头
Public Sub New(ByVal绑定为System.ServiceModel.Channels.binding,ByVal remoteAddress为System.ServiceModel.EndpointAddress)
MyBase.New(绑定,远程地址)
端接头
作为布尔值的公共函数OpenServ(ByVal n1为整数)实现了ICalculatorClient.OpenServ
返回MyBase.Channel.Openserv(n1)
端函数
末级
公共类客户端
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
Dim bld为新System.ServiceModel.NetNamedPipeBinding
Dim epa作为新的System.ServiceModel.EndpointAddress(“net。pipe://localhost/servicemodelsamples/service")
作为新计算器客户端的Dim客户端(bld、epa)
尺寸值1为整数=33
Dim结果为Boolean=client.OpenServ(值1)
client.Close()
信息框显示(“信息框”)
端接头
末级
现在的问题是:当客户机是由应用程序服务生成的codedom程序集时,如何执行此示例?类似于:向应用程序服务的主窗体添加一个按钮,并添加以下文本:
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
cr_text()
cr_app()
End Sub
Private Sub cr_text()
str_tb = "Imports System" & Chr(13) & Chr(10) & _
"Imports System.CodeDom" & Chr(13) & Chr(10) & _
"Imports System.CodeDom.Compiler" & Chr(13) & Chr(10) & _
"Imports System.Collections" & Chr(13) & Chr(10) & _
"Imports System.ComponentModel" & Chr(13) & Chr(10) & _
"Imports System.Diagnostics" & Chr(13) & Chr(10) & _
"Imports System.Windows.Forms" & Chr(13) & Chr(10) & _
"Imports System.Data" & Chr(13) & Chr(10) & _
"Imports System.Windows" & Chr(13) & Chr(10) & _
"Imports System.Data.SqlClient" & Chr(13) & Chr(10) & _
"Imports Microsoft.VisualBasic" & Chr(13) & Chr(10) & _
"Imports System.IO" & Chr(13) & Chr(10) & _
"Imports System.Drawing" & Chr(13) & Chr(10) & _
"Public Class MainClass" & Chr(13) & Chr(10) & _
"Public Shared Sub Main()" & Chr(13) & Chr(10) & _
"Dim tf As New test34" & Chr(13) & Chr(10) & _
"tf.show()" & Chr(13) & Chr(10) & _
"End Sub" & Chr(13) & Chr(10) & _
"End Class" & Chr(13) & Chr(10) & _
" <System.ServiceModel.ServiceContract()> _" & Chr(13) & Chr(10) & _
" Public Interface ICalculator" & Chr(13) & Chr(10) & _
" <System.ServiceModel.OperationContract()> _" & Chr(13) & Chr(10) & _
" Function Openserv(ByVal n1 As Integer) As Boolean" & Chr(13) & Chr(10) & _
" End Interface" & Chr(13) & Chr(10) & _
" Partial Public Class CalculatorClient" & Chr(13) & Chr(10) & _
" Inherits System.ServiceModel.ClientBase(Of ICalculator)" & Chr(13) & Chr(10) & _
" Implements ICalculator" & Chr(13) & Chr(10) & _
" Public Sub New()" & Chr(13) & Chr(10) & _
" MyBase.New()" & Chr(13) & Chr(10) & _
" End Sub" & Chr(13) & Chr(10) & _
" Public Sub New(ByVal endpointConfigurationName As String)" & Chr(13) & Chr(10) & _
" MyBase.New(endpointConfigurationName)" & Chr(13) & Chr(10) & _
" End Sub" & Chr(13) & Chr(10) & _
" Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As String)" & Chr(13) & Chr(10) & _
" MyBase.New(endpointConfigurationName, remoteAddress)" & Chr(13) & Chr(10) & _
" End Sub" & Chr(13) & Chr(10) & _
" Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As System.ServiceModel.EndpointAddress)" & Chr(13) & Chr(10) & _
" MyBase.New(endpointConfigurationName, remoteAddress)" & Chr(13) & Chr(10) & _
" End Sub" & Chr(13) & Chr(10) & _
" Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding, ByVal remoteAddress As System.ServiceModel.EndpointAddress)" & Chr(13) & Chr(10) & _
" MyBase.New(binding, remoteAddress)" & Chr(13) & Chr(10) & _
" End Sub" & Chr(13) & Chr(10) & _
" Public Function OpenServ(ByVal n1 As Integer) As Boolean Implements ICalculator.Openserv" & Chr(13) & Chr(10) & _
" Return MyBase.Channel.Openserv(n1)" & Chr(13) & Chr(10) & _
" End Function" & Chr(13) & Chr(10) & _
" End Class" & Chr(13) & Chr(10) & _
" <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _" & Chr(13) & Chr(10) & _
"Partial Class test34" & Chr(13) & Chr(10) & _
" Inherits System.Windows.Forms.Form" & Chr(13) & Chr(10) & _
" <System.Diagnostics.DebuggerNonUserCode()> _" & Chr(13) & Chr(10) & _
" Protected Overrides Sub Dispose(ByVal disposing As Boolean)" & Chr(13) & Chr(10) & _
" Try" & Chr(13) & Chr(10) & _
" If Disposing AndAlso components IsNot Nothing Then" & Chr(13) & Chr(10) & _
" components.Dispose()" & Chr(13) & Chr(10) & _
" End If" & Chr(13) & Chr(10) & _
" Finally" & Chr(13) & Chr(10) & _
" MyBase.Dispose(Disposing)" & Chr(13) & Chr(10) & _
" End Try" & Chr(13) & Chr(10) & _
" End Sub" & Chr(13) & Chr(10) & _
" Private components As System.ComponentModel.IContainer" & Chr(13) & Chr(10) & _
" <System.Diagnostics.DebuggerStepThrough()> _" & Chr(13) & Chr(10) & _
" Private Sub InitializeComponent()" & Chr(13) & Chr(10) & _
" Me.Button1 = New System.Windows.Forms.Button" & Chr(13) & Chr(10) & _
" Me.Button3 = New System.Windows.Forms.TextBox" & Chr(13) & Chr(10) & _
" Me.SuspendLayout()" & Chr(13) & Chr(10) & _
" Me.Button1.Location = New System.Drawing.Point(114, 40)" & Chr(13) & Chr(10) & _
" Me.Button1.Name = " & Chr(34) & "Button1" & Chr(34) & "" & Chr(13) & Chr(10) & _
" Me.Button1.Size = New System.Drawing.Size(231, 39)" & Chr(13) & Chr(10) & _
" Me.Button1.TabIndex = 0" & Chr(13) & Chr(10) & _
" Me.Button1.Text = " & Chr(34) & "Button1" & Chr(34) & "" & Chr(13) & Chr(10) & _
" Me.Button1.UseVisualStyleBackColor = True" & Chr(13) & Chr(10) & _
" Me.Button3.Location = New System.Drawing.Point(114, 90)" & Chr(13) & Chr(10) & _
" Me.Button3.Name = " & Chr(34) & "Button3" & Chr(34) & "" & Chr(13) & Chr(10) & _
" Me.Button3.Size = New System.Drawing.Size(231, 39)" & Chr(13) & Chr(10) & _
" Me.Button3.TabIndex = 2" & Chr(13) & Chr(10) & _
" Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)" & Chr(13) & Chr(10) & _
" Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font" & Chr(13) & Chr(10) & _
" Me.ClientSize = New System.Drawing.Size(465, 133)" & Chr(13) & Chr(10) & _
" Me.Controls.Add(Me.Button1)" & Chr(13) & Chr(10) & _
" Me.Controls.Add(Me.Button3)" & Chr(13) & Chr(10) & _
" Me.Name = " & Chr(34) & "test" & Chr(34) & "" & Chr(13) & Chr(10) & _
" Me.Text = " & Chr(34) & "test" & Chr(34) & "" & Chr(13) & Chr(10) & _
" Me.ResumeLayout(False)" & Chr(13) & Chr(10) & _
" End Sub" & Chr(13) & Chr(10) & _
" Friend WithEvents Button1 As System.Windows.Forms.Button" & Chr(13) & Chr(10) & _
" Friend WithEvents Button3 As System.Windows.Forms.TextBox" & Chr(13) & Chr(10) & _
"End Class" & Chr(13) & Chr(10) & _
"Public Class test34" & Chr(13) & Chr(10) & _
" Dim str_tb As String" & Chr(13) & Chr(10) & _
" Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click" & Chr(13) & Chr(10) & _
" Dim bld As New System.ServiceModel.NetNamedPipeBinding" & Chr(13) & Chr(10) & _
" Dim epa As New System.ServiceModel.EndpointAddress(" & Chr(34) & "net.pipe://localhost/servicemodelsamples/service" & Chr(34) & ")" & Chr(13) & Chr(10) & _
" Dim client As New CalculatorClient(bld, epa)" & Chr(13) & Chr(10) & _
" Dim value1 As Integer = Button3.Text" & Chr(13) & Chr(10) & _
" Dim result As Boolean = client.OpenServ(value1)" & Chr(13) & Chr(10) & _
" client.Close()" & Chr(13) & Chr(10) & _
" MessageBox.Show(" & Chr(34) & "Открыто в основном приложении" & Chr(34) & ")" & Chr(13) & Chr(10) & _
" End Sub" & Chr(13) & Chr(10) & _
"End Class"
End Sub
Private Shared Sub cr_app()
Dim objCodeCompiler As System.CodeDom.Compiler.CodeDomProvider = System.CodeDom.Compiler.CodeDomProvider.CreateProvider("VB")
Dim objCompilerParameters As New System.CodeDom.Compiler.CompilerParameters()
objCompilerParameters.ReferencedAssemblies.Add("mscorlib.dll")
objCompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll")
objCompilerParameters.ReferencedAssemblies.Add("System.dll")
objCompilerParameters.ReferencedAssemblies.Add("C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\v3.0\System.ServiceModel.dll")
objCompilerParameters.ReferencedAssemblies.Add("System.Xml.dll")
objCompilerParameters.ReferencedAssemblies.Add("System.Data.dll")
objCompilerParameters.ReferencedAssemblies.Add("System.Drawing.dll")
objCompilerParameters.ReferencedAssemblies.Add("Microsoft.VisualBasic.dll")
objCompilerParameters.CompilerOptions = "/target:winexe"
objCompilerParameters.GenerateExecutable = True
objCompilerParameters.GenerateInMemory = True
objCompilerParameters.IncludeDebugInformation = False
Dim objCompileResults As System.CodeDom.Compiler.CompilerResults = objCodeCompiler.CompileAssemblyFromSource(objCompilerParameters, str_tb)
If objCompileResults.Errors.HasErrors Then
MessageBox.Show(String.Format("Error: Line>{0}, {1} # {2}", objCompileResults.Errors(0).Line, objCompileResults.Errors(0).ErrorText, objCompileResults.Errors(0).ErrorNumber))
Return
End If
Dim objAssembly As System.Reflection.Assembly = objCompileResults.CompiledAssembly
Dim objTheClass As Object = objAssembly.CreateInstance("MainClass")
If objTheClass Is Nothing Then
MsgBox("Can't load class...")
Exit Sub
End If
Try
objTheClass.GetType.InvokeMember("Main", System.Reflection.BindingFlags.InvokeMethod, _
Nothing, objTheClass, Nothing)
Catch ex As Exception
MsgBox("Error:" & ex.Message)
End Try
End Sub
Private子按钮5\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮5。单击
cr_text()
cr_app()
端接头
私有子cr_文本()
str_tb=“导入系统”&Chr(13)&Chr(10)&_
“Imports System.CodeDom”&Chr(13)&Chr(10)&_
“导入系统.CodeDom.Compiler”&Chr(13)&Chr(10)&_
“导入系统集合”&Chr(13)&Chr(10)&_
“导入系统组件模型”&Chr(13)&Chr(10)&_
“导入系统诊断”&Chr(13)&Chr(10)&_
“导入系统.Windows.Forms”&Chr(13)&Chr(10)&_
“导入系统数据”&Chr(13)&Chr(10)&_
“导入系统.Windows”&Chr(13)&Chr(10)&_
“导入System.Data.SqlClient”&Chr(13)&Chr(10)&_
“导入Microsoft.VisualBasic”&Chr(13)&Chr(10)&_
“Imports System.IO”&Chr(13)&Chr(10)&_
“导入系统图纸”&Chr(13)&Chr(10)&_
“公共类主类”和Chr(13)和Chr(10)以及_
“公共共享子干线()”&Chr(13)&Chr(10)&_
“作为新测试的Dim tf 34”和Chr(13)和Chr(10)以及_
“tf.show()”&Chr(13)&Chr(10)&_
“末端接头”&Chr(13)&Chr(10)&_
“最终类别”&Chr(13)&Chr(10)&_
"条例"及条例(第13条)及条例(第10条)及_
“公共接口ICalculator”&Chr(13)&Chr(10)&_
"条例"及条例(第13条)及条例(第10条)及_
“函数Openserv(ByVal n1作为整数)作为布尔值”&Chr(13)&Chr(10)&_
“终端接口”&Chr(13)&Chr(10)&_
“部分公共类计算器客户端”&Chr(13)&Chr(10)&_
“继承System.ServiceModel.ClientBase(ICalculator的)”&Chr(13)&Chr(10)&_
“实现ICalculator”&Chr(13)&Chr(10)&_
"浦
str_tb = "Imports System" & Chr(13) & Chr(10) & _
Dim sbCode As New System.Text.StringBuilder(100000)
sbCode.Append("Imports System").AppendLine()