当网站托管在 IIS 中时获取 MicrosoftOnlineException

Getting MicrosoftOnlineException when website is hosted in IIS

有一个从 asp.net 调用的 powershell 脚本。 powershell 基本上连接到 Windows Azure 模块。

当我从 VS 调试 asp.net 时,它按预期托管在 IIs-express 和 powershell 脚本 运行s 中。 但是当网站托管在 IIS 中时,会抛出以下异常

System.Management.Automation.InvalidPowerShellStateException occurred
  HResult=-2146233087
  Message=Microsoft.Online.Administration.Automation.MicrosoftOnlineException: Exception of type 'Microsoft.Online.Administration.Automation.MicrosoftOnlineException' was thrown.
Microsoft.Online.Administration.Automation.MicrosoftOnlineException: You must call the Connect-MsolService cmdlet before calling any other cmdlets.

Server stack trace: 
   at Microsoft.Online.Administration.Automation.BecWebServiceInspector.BeforeSendRequest(Message& request, IClientChannel channel)
   at System.ServiceModel.Dispatcher.ImmutableClientRuntime.BeforeSendRequest(ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.PrepareCall(ProxyOperationRuntime operation, Boolean oneway, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at IProvisioningWebService.GetRoleByName(GetRoleByNameRequest request)
   at ProvisioningWebServiceClient.GetRoleByName(GetRoleByNameRequest request)
System.Management.Automation.ParameterBindingException: Cannot bind parameter 'RoleObjectId' to the target. Exception setting "RoleObjectId": "Object reference not set to an instance of an object." ---> System.Management.Automation.SetValueInvocationException: Exception setting "RoleObjectId": "Object reference not set to an instance of an object." ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , Object , Object )
   at System.Management.Automation.ReflectionParameterBinder.BindParameter(String name, Object value)
   --- End of inner exception stack trace ---
   at System.Management.Automation.ReflectionParameterBinder.BindParameter(String name, Object value)
   at System.Management.Automation.ParameterBinderBase.BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags)
   --- End of inner exception stack trace ---
   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)
   at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
   at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

  Source=ManagementConsoleV2
  StackTrace:
       at ManagementConsoleV2.Structures.Lync_Online.PowershellWrapper.RunPowerShellScript(String userName, String plainPassword, PowerShellScriptType powerShellScriptType) in c:\Dev Team\Sameer\Trunk\Web\ManagementConsoleV2\Structures\Lync Online\PowershellWrapper.cs:line 64
  InnerException: 

当应用程序在 IIS-express 中 运行 时,在 32 位模式下进程 运行s。我还在 IIS 的两个 32 bit/64 位模式线程池中托管了该应用程序,但两次都出现了相同的错误。

在 IIS 中 转到 高级设置 中您的站点设置为 运行 的应用程序线程池并设置 加载用户配置文件:true