C#CLR用户定义函数SqlBytes-InvalidOperationException

C#CLR用户定义函数SqlBytes-InvalidOperationException,c#,sql-server-2008,clr,filestream,invalidoperationexception,C#,Sql Server 2008,Clr,Filestream,Invalidoperationexception,我一直在编写一个sql clr udf,它是从存储过程调用的,用于将未知文件类型(jpg、doc、pdf等)保存到文件系统。UFD接受SqlBytes类型的参数,该参数从调用的存储过程(这是文件blob)传递一个varbinary(max)。问题是,在CLR UDF的上下文中,我无法访问SqlBytes文件param的value属性,甚至无法访问读取方法,因为它返回以下无效异常错误 WindowsImpersonationContext newContext = null; WindowsId


我一直在编写一个sql clr udf,它是从存储过程调用的,用于将未知文件类型(jpg、doc、pdf等)保存到文件系统。UFD接受SqlBytes类型的参数,该参数从调用的存储过程(这是文件blob)传递一个varbinary(max)。问题是,在CLR UDF的上下文中,我无法访问SqlBytes文件param的value属性,甚至无法访问读取方法,因为它返回以下无效异常错误

WindowsImpersonationContext newContext = null;

WindowsIdentity newIdentity = SqlContext.WindowsIdentity;
我缩短了我的UDF,只是为了强调提到的问题。任何帮助都将不胜感激

WindowsImpersonationContext newContext = null;

WindowsIdentity newIdentity = SqlContext.WindowsIdentity;
提前谢谢

WindowsImpersonationContext newContext = null;

WindowsIdentity newIdentity = SqlContext.WindowsIdentity;

错误 在此上下文中不允许数据访问。上下文是未标记为
DataAccessKind.Read
SystemDataAccessKind.Read
的函数或方法,是从表值函数的FillRow方法获取数据的回调,或者是UDT验证方法

WindowsImpersonationContext newContext = null;

WindowsIdentity newIdentity = SqlContext.WindowsIdentity;

堆栈跟踪
CLR代码
为了完成这个线程,这里是CLR UDF的基本POC代码,它接受varbinary(max)文件blob、文件名、文件扩展名和写入路径,然后将其保存到定义的文件系统位置(前提是它具有适当的文件系统权限)。希望它能帮助别人:-)

WindowsImpersonationContext newContext = null;

WindowsIdentity newIdentity = SqlContext.WindowsIdentity;

我发现,我在写文件系统测试时留下的是我在其中的模拟语句

WindowsImpersonationContext newContext = null;

WindowsIdentity newIdentity = SqlContext.WindowsIdentity;
如果我删除了下面的代码行,那么所有代码都可以正常工作

WindowsImpersonationContext newContext = null;

WindowsIdentity newIdentity = SqlContext.WindowsIdentity;

WindowsImpersonationContext newContext = null;

WindowsIdentity newIdentity = SqlContext.WindowsIdentity;

我发现里面有一些模仿的陈述。如果删除“WindowsImpersonationContext newContext=null;”和“WindowsIdentity newIdentity=SqlContext.WindowsIdentity;”和“if(newIdentity!=null)newContext=newIdentity.Impersonate();”它起作用了