花了一些时间来解决这个问题,结论很有意思。
我们的Office(Word / Excel / PowerPoint)加载项向我们的自定义WCF服务发送请求,托管Office应用程序终止,将此条目留在应用程序日志中:
Provider: .NET Runtime
EventID: 1023
Level: 2
Task: 0
Keywords: 0x80000000000000
Channel: Application
EventData: .NET Runtime version 2.0.50727.4200 - Fatal Execution Engine Error (6BC47B3E) (80131506)
要重现此操作,请在Visual Studio 2008中创建一个新的“Word 2007加载项”项目。添加对System.ServiceModel和System.Runtime.Serialization的引用。修改ThisAddin类以包含此代码,我认为这是重现此行为所需的最少代码:
[Serializable]
public class CustomQuery { }
[Serializable]
public class CustomQueryCollection : ReadOnlyCollection<CustomQuery>
{
public CustomQueryCollection(IEnumerable<CustomQuery> queries)
: base(queries.ToArray())
{ }
}
[Serializable]
[KnownType(typeof(CustomQueryCollection))]
public class CustomRequest : ISerializable
{
readonly CustomQueryCollection _collection;
public CustomRequest(IEnumerable<CustomQuery> queries)
{
_collection = new CustomQueryCollection(queries);
}
protected CustomRequest(SerializationInfo info, StreamingContext context)
{
_collection = (CustomQueryCollection)info.GetValue("Queries", typeof(CustomQueryCollection));
}
public CustomQueryCollection Queries { get { return _collection; } }
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Queries", _collection);
}
}
[ServiceContract]
public interface ICustomService
{
[OperationContract]
void SendRequest(CustomRequest request);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class CustomService : ICustomService
{
public void SendRequest(CustomRequest request)
{
// this line is never reached.
}
}
public class CustomClient : ClientBase<ICustomService>, ICustomService
{
public CustomClient(Binding binding, EndpointAddress address)
: base(binding, address)
{ }
public void SendRequest(CustomRequest request)
{
Channel.SendRequest(request);
}
}
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
var address = "net.pipe://localhost/kamikaze";
var endpointAddress = new EndpointAddress(address);
var binding = new NetNamedPipeBinding();
using (var serviceHost = new ServiceHost(new CustomService()))
using (var client = new CustomClient(binding, endpointAddress))
{
serviceHost.AddServiceEndpoint(typeof(ICustomService), binding, address);
serviceHost.Open();
client.SendRequest(new CustomRequest(new CustomQuery[0]));
// this line is never reached.
serivceHost.Close();
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { }
#region VSTO generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
点击[F5]:Word 2007启动,然后消失,将上述日志消息留在系统的应用程序日志中。相同的代码在我们尝试过的所有其他上下文中都能正常工作。
答案 0 :(得分:0)
更改以下代码行:
[KnownType(typeof(CustomQueryCollection))]
[KnownType(typeof(CustomQuery[]))]
_collection = (CustomQueryCollection)info.GetValue("Queries", typeof(CustomQueryCollection));
_collection = new CustomQueryCollection((CustomQuery[]) info.GetValue("Queries", typeof(CustomQuery[])));
info.AddValue("Queries", _collection);
info.AddValue("Queries", _collection.ToArray());
...那里。没有致命的引擎执行错误。我怀疑这与Office是.Net 2.0主机有关,而我们所有其他用例和测试都涉及.Net 3.5主机,但我只是在猜测。