从 WCF 服务运行交换命令时出错



所以我做了这个应用程序,它运行交换命令并将结果发送到SQL服务器,在某种程度上是服务器维护。我将在本地运行交换命令的函数作为一个类运行,为了在多台计算机上部署,应用程序应连接到调用类中函数的 WCF 服务。我尝试将本地函数移动到 WCF 服务文件中,并将类移动到 WCF 服务文件引用的较大类中。现在我收到一个错误,即尽管交换命令在本地运行时可以正常工作,但它仍无法识别。运行空间和电源外壳命令执行是否不适用于 WCF 服务,或者我是否设置了错误的项目。

我正在将我的应用程序部署到 64 位计算机。WCF 服务引用的类也是在 64 位计算机上生成的。

Public Sub GetExchangedata(strComp As String, strServ As String)
Dim strMailboxes() As String = {"DiscoveryMailbox", "EquipmentMailbox", "GroupMailbox", "LinkedMailbox", "LegacyMailbox", "RoomMailbox", "SharedMailbox", "SchedulingMailbox", "TeamMailbox", "UserMailbox"}
Dim MyStringBuiler As New StringBuilder()
Dim rsConfig As RunspaceConfiguration = RunspaceConfiguration.Create()
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", New PSSnapInException)
For Each strMailboxtype As String In strMailboxes
Using myRunSpace As Runspace = RunspaceFactory.CreateRunspace(rsConfig)
myRunSpace.Open()
Dim mypipeline As Pipeline = myRunSpace.CreatePipeline()
Dim rsInvoker As RunspaceInvoke = New RunspaceInvoke(myRunSpace)
rsInvoker.Invoke("Set-ExecutionPolicy Bypass")
Dim exchCommand As String = "Add-PSSnapin 
Microsoft.Exchange.Management.PowerShell.E2010;(get-mailbox - 
recipienttypedetails " & strMailboxtype & " ).count "
Dim strProc As String = "(get-process).count" 'this command works 
mypipeline.Commands.AddScript(exchCommand)
'mypipeline.Commands.AddScript(strProc)
mypipeline.Commands.Add("Out-String")
Dim outputs As Collection(Of PSObject) = mypipeline.Invoke()
For Each result As PSObject In outputs
MyStringBuiler.AppendLine(strMailboxtype & " : " & result.ToString())
dsExchangeAcounts.Tables(0).Rows.Add(strComp, strServ, strMailboxtype, result.ToString)
Next
rsInvoker.Invoke("Set-ExecutionPolicy Default")
myRunSpace.Close()
End Using
Next
End Sub

此代码在我的本地计算机上完美运行,但从 WCF 服务客户端运行时出错。

因此,错误原来是我的 WCF 服务应用程序未在 64 位模式下运行,而 powershell 不喜欢这种 32 位模式。点击此链接: WCF 64 位不工作

将其配置为仅以 64 位运行后,我的代码完美执行。棒

最新更新