C# 无法通过WCF数据服务中的浏览器查看动态添加的数据
我已经为动态数据提供者实现了WCF数据服务。我正在使用内存中的虚拟数据源。我的实现基于Alex D James的精彩系列: 我的问题是,我能够通过客户端应用程序添加产品对象,并且响应也显示添加了该对象,但当我尝试通过客户端应用程序的产品密钥或通过浏览器获取数据时,我收到一个错误,显示“未找到段数据源记录的资源” 我有如下初始化方法:C# 无法通过WCF数据服务中的浏览器查看动态添加的数据,c#,odata,wcf-data-services,C#,Odata,Wcf Data Services,我已经为动态数据提供者实现了WCF数据服务。我正在使用内存中的虚拟数据源。我的实现基于Alex D James的精彩系列: 我的问题是,我能够通过客户端应用程序添加产品对象,并且响应也显示添加了该对象,但当我尝试通过客户端应用程序的产品密钥或通过浏览器获取数据时,我收到一个错误,显示“未找到段数据源记录的资源” 我有如下初始化方法: config.SetEntitySetAccessRule("*", EntitySetRights.All); config.Se
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
config.DataServiceBehavior.AcceptCountRequests = true;
config.DataServiceBehavior.AcceptProjectionRequests = true;
我使用动态数据提供程序作为:OrganizationDataService:DataService,IServiceProvider
其中OrganizationDataServiceProvider是扩展IDataServiceMetadataProvider并利用我的OrganizationServiceContext实现:
public class OrganizationServiceContext
{
private List<DummyDataSource> records = new List<DummyDataSource>();
private Dictionary<string, ResourceType> resourceTypes = new Dictionary<string, ResourceType>();
private Dictionary<string, ResourceSet> resourceSets = new Dictionary<string, ResourceSet>();
public OrganizationServiceContext()
{
PopulateMedataData(this);
}
private void AddResourceType(ResourceType resourceType)
{
//resourceType.SetReadOnly();
resourceTypes.Add(resourceType.FullName, resourceType);
}
private void AddResourceSet(ResourceSet resourceSet)
{
resourceSet.SetReadOnly();
resourceSets.Add(resourceSet.Name, resourceSet);
}
private void PopulateMedataData(OrganizationServiceContext orgServiceContext)
{
var productType = new ResourceType(
typeof(DummyDataSource), // CLR type backing this Resource
ResourceTypeKind.EntityType, // Entity, ComplexType etc
null, // BaseType
"Namespace", // Namespace
"DummyDataSource", // Name
false // Abstract?
);
var prodKey = new ResourceProperty(
"ProdKey",
ResourcePropertyKind.Key |
ResourcePropertyKind.Primitive,
ResourceType.GetPrimitiveResourceType(typeof(int))
);
var prodName = new ResourceProperty(
"Name",
ResourcePropertyKind.Primitive,
ResourceType.GetPrimitiveResourceType(typeof(string))
);
var prodPrice = new ResourceProperty(
"Price",
ResourcePropertyKind.Primitive,
ResourceType.GetPrimitiveResourceType(typeof(Decimal))
);
productType.AddProperty(prodKey);
productType.AddProperty(prodName);
productType.AddProperty(prodPrice);
orgServiceContext.AddResourceType(productType);
orgServiceContext.AddResourceSet(new ResourceSet("DataSourceRecords", productType));
}
public void PopulateContextData(OrganizationServiceContext orgServiceContext)
{
orgServiceContext.Records.Add(
new DummyDataSource
{
ProdKey = 1,
Name = "Janus",
Cost = 4.35M,
Price = 6.49M
});
orgServiceContext.Records.Add(
new DummyDataSource
{
ProdKey = 2,
Name = "Bugger",
Cost = 4.97M,
Price = 7.21M
});
}
public Dictionary<string, ResourceType> GetResourceType()
{
return this.resourceTypes;
}
public Dictionary<string, ResourceSet> GetResourceSet()
{
return this.resourceSets;
}
public IQueryable GetQueryable(ResourceSet set)
{
if (set.Name == "DataSourceRecords")
{
return this.Records.AsQueryable();
}
else
{
throw new NotSupportedException(string.Format("{0} not found", set.Name));
}
}
public List<DummyDataSource> Records
{
get
{
return this.records;
}
}
public object CreateResource(ResourceType resourceType)
{
if (resourceType.InstanceType == typeof(DummyDataSource))
{
Debug.WriteLine(string.Format("OrganizationServiceContext : CreateResource::If. resourceType = {0}, Context.HashCode = {1}", resourceType.InstanceType, this.GetHashCode()));
return new DummyDataSource();
}
else
{
Debug.WriteLine(string.Format("OrganizationServiceContext : CreateResource::Else. resourceType = {0}, Context.HashCode = {1}", resourceType.InstanceType, this.GetHashCode()));
throw new NotSupportedException(string.Format("{0} not found", resourceType.FullName));
}
}
public void AddResource(ResourceType resourceType, object resource)
{
if (resourceType.InstanceType == typeof(DummyDataSource))
{
DummyDataSource temp = resource as DummyDataSource;
if (temp != null)
{
Debug.WriteLine(string.Format("OrganizationServiceContext : AddResource. resourceType = {0}, resource = {1}, Context.HashCode = {2}", resourceType, resource, this.GetHashCode()));
this.Records.Add(temp);
//Below foreach loop is only for debug purpose, will be removed from final code.
foreach (var pr in Records)
{
Debug.WriteLine(string.Format("Prod Key = {0}, Name = {1}, Price = {2}", pr.ProdKey, pr.Name, pr.Price));
}
return;
}
}
else
{
throw new NotSupportedException("Type not found");
}
}
public void DeleteResource( object resource)
{
if (resource.GetType() == typeof(DummyDataSource))
{
this.Records.Remove(resource as DummyDataSource);
return;
}
else
{
throw new NotSupportedException("Type not found");
}
}
public void SaveChanges()
{
Debug.WriteLine(string.Format("OrganizationServiceContext : SaveChanges. Context.HashCode = {0}", this.GetHashCode()));
var prodKey = this.Records.Max(p => p.ProdKey);
foreach (var prod in this.Records.Where(p => p.ProdKey == 0))
{
prod.ProdKey = prodKey+1;
}
//Below foreach loop is only for debug purpose, will be removed from final code.
foreach (var pr in Records)
{
Debug.WriteLine(string.Format("Prod Key = {0}, Name = {1}, Price = {2}", pr.ProdKey, pr.Name, pr.Price));
}
}
}
公共类OrganizationServiceContext
{
私有列表记录=新列表();
私有字典resourceTypes=新字典();
私有字典资源集=新字典();
公共组织ServiceContext()
{
人口数据(本);
}
私有void AddResourceType(ResourceType ResourceType)
{
//resourceType.SetReadOnly();
添加(resourceType.FullName,resourceType);
}
私有void AddResourceSet(ResourceSet ResourceSet)
{
SetReadOnly();
添加(resourceSet.Name,resourceSet);
}
私有void populateMetadata(OrganizationServiceContext或ServiceContext)
{
var productType=新资源类型(
typeof(DummyDataSource),//支持此资源的CLR类型
ResourceTypeKind.EntityType、//实体、ComplexType等
null,//BaseType
“名称空间”,//名称空间
“DummyDataSource”,//名称
假//抽象?
);
var prodKey=新资源属性(
“ProdKey”,
ResourcePropertyKind.Key|
ResourcePropertyKind.Primitive,
GetPrimitiveResourceType(typeof(int))
);
var prodName=新资源属性(
“姓名”,
ResourcePropertyKind.Primitive,
GetPrimitiveResourceType(typeof(string))
);
var prodPrice=新资源属性(
“价格”,
ResourcePropertyKind.Primitive,
GetPrimitiveResourceType(typeof(Decimal))
);
productType.AddProperty(prodKey);
productType.AddProperty(prodName);
productType.AddProperty(产品价格);
orgServiceContext.AddResourceType(productType);
AddResourceSet(新资源集(“数据源记录”,productType));
}
public void PopulateContextData(OrganizationServiceContext或ServiceContext)
{
orgServiceContext.Records.Add(
新DummyDataSource
{
ProdKey=1,
Name=“Janus”,
成本=435万美元,
价格=649万美元
});
orgServiceContext.Records.Add(
新DummyDataSource
{
ProdKey=2,
Name=“Bugger”,
成本=497万美元,
价格=721万美元
});
}
公共字典GetResourceType()
{
返回this.resourceTypes;
}
公共字典GetResourceSet()
{
返回此文件。resourceSets;
}
公共IQueryable GetQueryable(资源集)
{
if(set.Name==“数据源记录”)
{
返回此.Records.AsQueryable();
}
其他的
{
抛出新的NotSupportedException(string.Format(“{0}未找到”,set.Name));
}
}
公开名单记录
{
得到
{
将此文件归还给您的记录;
}
}
公共对象CreateResource(ResourceType ResourceType)
{
if(resourceType.InstanceType==typeof(DummyDataSource))
{
Debug.WriteLine(string.Format(“OrganizationServiceContext:CreateResource::If.resourceType={0},Context.HashCode={1}”,resourceType.InstanceType,this.GetHashCode());
返回新的DummyDataSource();
}
其他的
{
Debug.WriteLine(string.Format(“OrganizationServiceContext:CreateResource::Else.resourceType={0},Context.HashCode={1}”,resourceType.InstanceType,this.GetHashCode());
抛出新的NotSupportedException(string.Format(“{0}未找到”,resourceType.FullName));
}
}
public void AddResource(资源类型ResourceType,对象资源)
{
if(resourceType.InstanceType==typeof(DummyDataSource))
{
DummyDataSource temp=作为DummyDataSource的资源;
如果(温度!=null)
{
WriteLine(string.Format(“OrganizationServiceContext:AddResource.resourceType={0},resource={1},Context.HashCode={2}”,resourceType,resource,this.GetHashCode());
此.Records.Add(temp);
//下面的foreach循环仅用于调试目的,将从最终代码中删除。
foreach(记录中的var pr)
{
Debug.WriteLine(string.Format(“Prod Key={0},Name={1},Price={2}”,pr.ProdKey,pr.Name,pr.Price));
}
返回;
}
}
其他的
{
抛出新的NotSupportedException(“找不到类型”);
}
}
公共资源(对象资源)
{
if(resource.GetType()==typeof(DummyDataSource))
{
this.Records.Remove(作为DummyDataSource的资源);
返回;
}
其他的
{
抛出新的NotSupportedException(“找不到类型”);
}
}
公共void SaveChanges()
{
WriteLine(string.Format(“OrganizationServiceContext:SaveChanges.Context.HashCode={0}”),t