これを解決するのに少し時間がかかり、結論は興味深いものです。
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)
これを再現するには、VisualStudio2008で新しい「Word2007アドイン」プロジェクトを作成します。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が起動してから消え、上記のログメッセージがシステムのアプリケーションログに残ります。同じコードは、私たちが試した他のすべてのコンテキストで完全に正常に機能します。