高度に保守可能な WCF サービスの記述に基づく WCF サービスがあります。リクエストは CommandService を使用して処理されます。
[WcfDispatchBehaviour]
[ServiceContract(Namespace="http://somewhere.co.nz/NapaWcfService/2013/11")]
[ServiceKnownType("GetKnownTypes")]
public class CommandService
{
[OperationContract]
public object Execute(dynamic command)
{
Type commandHandlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());
dynamic commandHandler = BootStrapper.GetInstance(commandHandlerType);
commandHandler.Handle(command);
return command;
}
public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)
{
var coreAssembly = typeof(ICommandHandler<>).Assembly;
var commandTypes =
from type in coreAssembly.GetExportedTypes()
where type.Name.EndsWith("Command")
select type;
return commandTypes.ToArray();
}
}
すべてがうまく機能します (Steve に感謝します) が、サービスにファイルをアップロードする機能を追加する必要があります。私が読んだことと、テスト中に受け取ったエラーに基づいて、WCF は を使用[MessageContract]
してファイルをアップロードするときに を使用する必要がありますStream
。そのため、コマンド クラスを装飾し、ストリーム以外のメンバーをメッセージ ヘッダーに配置し、ストリーミングを使用するようにバインディング定義を更新しました。
[MessageContract]
public class AddScadaTileCommand
{
[MessageHeader(MustUnderstand = true)]
public int JobId { get; set; }
[MessageHeader(MustUnderstand = true)]
public string MimeType { get; set; }
[MessageHeader(MustUnderstand = true)]
public string Name { get; set; }
[MessageBodyMember(Order = 1)]
public Stream Content { get; set; }
}
残念ながら、アップロードするファイルでサービスを呼び出すと、エラーが発生します。
パラメータ http://somewhere.co.nz/NapaWcfService/2013/11:commandをシリアル化しようとしているときにエラーが発生しました。InnerException メッセージは、「Type 'System.IO.FileStream' with data contract name 'FileStream: http://schemas.datacontract.org/2004/07/System.IO ' is not expected.」 でした。
そこで、ファイル アップロード リクエスト専用の新しいメソッドをサービスに追加しました。
[OperationContract]
public void Upload(AddScadaTileCommand addScadaTileCommand)
{
Type commandHandlerType = typeof(ICommandHandler<>).MakeGenericType(typeof(AddScadaTileCommand));
dynamic commandHandler = BootStrapper.GetInstance(commandHandlerType);
commandHandler.Handle(addScadaTileCommand);
}
AddScadaTileCommand
メソッド定義でパラメーターを変更しない限り、これは完全に機能しますdynamic
。その場合、上記と同じエラーが発生します。これは、 をパラメーターの型として[MessageContract]
使用する場合、属性が適用されないか無視されることを示しているようです。dynamic
これを解決する方法はありますか? または、ストリームを含むリクエストに対して別のメソッドを作成する必要がありますか?