EDIT完全性のために私の質問が更新されました。汎用オブジェクトの作成常にヌルを返す
私は、iPHoneクライアントから着信RESTコールを受け取りました。ジェネリックリクエストに応じてタイプ固有のオブジェクト を消費することを意味します。たとえば:
http://localhost:81/dashboard/group/id/0
地域タイプからデータを返しますお客様から
http://localhost:81/dashboard/group/id/1
戻りデータは、ユーザーからの
http://localhost:81/dashboard/group/id/2
返すデータは
を入力し入力します10など。文字列GroupBase、Contracts.IDashboardService { プライベート文字列名=
パブリッククラスダッシュボード:
WCF Dashboard.svcサービスは、基本メソッドは、私が決定し、型特異的応答を返すために使用 をGetGroupById露出させます。空の;
public abstract class GroupBase
{
protected GroupBase() { }
public virtual Stream GetGroupById(string id)
{
// I have tried assigning response to null or, in this case,
// assigning it to a random service object. I have also tried
// IObjectFactory response; The last fails at compile-time and
// the other two always produce null
IObjectFactory response =
ObjectFactory<IObjectFactory, UserService>.Create();
var groupId = System.Convert.ToInt32(id);
var serializer = new JavaScriptSerializer();
byte[] bytes = null;
var message = String.Empty;
try
{
switch (groupId)
{
case 0: // regions
response = ObjectFactory<IObjectFactory, RegionService>.Create();
break;
case 1: // customers
response = ObjectFactory<IObjectFactory, CustomerService>.Create();
break;
case 2: // users
response = ObjectFactory<IObjectFactory, UserService>.Create();
break;
}
}
catch (EngageException oops)
{
message = oops.Message;
}
bytes = Encoding.UTF8.GetBytes(serializer.Serialize(response));
return new MemoryStream(bytes);
}
}
顧客のObjectFactoryクラスは次のとおりです。
public Dashboard() : base()
{
if (!ServiceSecurityContext.Current.PrimaryIdentity.IsAuthenticated)
throw new WebException("Unauthorized: Class: Dashboard, Method: Dashboard()",
System.Net.HttpStatusCode.Forbidden);
name = ServiceSecurityContext.Current.PrimaryIdentity.Name;
}
public override System.IO.Stream GetGroupById(string id)
{
return base.GetGroupById(id);
}
}
さて、私の抽象基底クラス内GetGroupByIdは を移入し、対応するグループIDパラメータに基づいて独自のデータ転送オブジェクトを返しスイッチ/ case文を持っていますタイプ固有のオブジェクトの作成に使用されます。
public static class ObjectFactory T:F、new() { public static F Create() { return new T();私は問題があります }}
は私のObjectFactoryのフードの下で何が起こっているのかです。私はいつも ** **ヌル**戻る。たとえば、以下のRESTのHTTP GET考える:上記のコマンド
http://localhost:81/dashboard/group/id/2
は、データベース内のすべてのユーザーのJSON文字列を求めています。したがって、 UserServiceクラスがObjectFactoryメソッドに渡されます。
public class UserService : IObjectFactory
{
DomainObjectsDto IObjectFactory.Children
{
get
{
return new Contracts.DomainObjectsDto(UserRepository
.GetAllUsers().Select
(p => new Contracts.DomainObjectDto
{
Title = GroupTypes.Customer.ToString(),
Id = p.CustomerId.ToString(),
Type = p.GetType().ToString()
}));
}
}
string IObjectFactory.Method
{
get;
set;
}
string IObjectFactory.Status
{
get;
set;
}
etc...
そして、読み取り専用プロパティはUserRepositoryからデータを取得し、取得し、データ転送オブジェクト (下記に示す)
[DataContract]
public class DomainObjectDto
{
[DataMember]
public string Title { get; set; }
[DataMember]
public string Id { get; set; }
[DataMember]
public string Type { get; set; }
}
[CollectionDataContract]
public class DomainObjectsDto : List<DomainObjectDto>
{
public DomainObjectsDto() { }
public DomainObjectsDto(IEnumerable<DomainObjectDto> source) : base(source) { }
}
を移入し、クライアントへのユーザーデータのシリアライズされたJSON文字列を返す必要があります。しかし、私のオブジェクトファクトリクラスの私のジェネリック型Tは常にnullです:
public static F Create()
{
return new T(); // <-- always null!
}
任意のアイデア?
あなたは私たちに少しのコードを与えました。問題を示す短くて完全なプログラムを投稿できれば、何が起こっているのかを知ることはずっと簡単です。 –
ありがとうございました。私は同意します:)私は完全な質問を再投稿しました(私は願っています)。他に何かを残しておいてくれたら、教えてください。 –
興味深い - すべてがバックエンドで正しく動作していると思われ、JSON文字列をクライアントに送信する必要がある場合、WCFはプロセス全体を再実行してnullを生成します。柔らかく、不必要なポストバックのようなもの –