Thursday, March 8, 2012

clr integration....

sir here are the functions on my test application...

<Microsoft.SqlServer.Server.SqlFunction()> _

Public Shared Function WeekInYear(ByVal dt As DateTime) As Integer

Return DatePart("ww", dt, FirstDayOfWeek.Monday, _

FirstWeekOfYear.FirstFourDays)

End Function

<Microsoft.SqlServer.Server.SqlFunction()> _

Public Shared Function HttpPost(ByVal uri As String, ByVal parameters As String) As String

Try

Dim urlRegEx As New System.Text.RegularExpressions.Regex("http://192.168.1.23:81/.*")

Dim p As New System.Net.WebPermission(System.Net.NetworkAccess.Connect, urlRegEx)

p.Assert()

Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(uri)

req.ContentType = "application/x-www-form-urlencoded;charset=utf-8"

req.Method = "POST"

Try

Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(parameters)

req.ContentLength = bytes.Length

Dim os As System.IO.Stream = req.GetRequestStream

os.Write(bytes, 0, bytes.Length)

'Push it out there

os.Close()

Catch ex As Exception

Throw New Exception(ex.Message)

End Try

Dim resp As System.Net.WebResponse = req.GetResponse

If resp Is Nothing Then

Return Nothing

End If

Return "pass completed"

Catch ex As Exception

Return ex.Message

Finally

End Try

End Function

the first function works fine, the second function doesn't work, it seems that if i ever use another namespace it requires a security permission.

can you specify why do i recieved this error at runtime?

Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

You have to give the assembly the appropiate permissions in SQL Server. By default only a few assemblies / namespaces are *trusted*. Try to give more access to the assembly by registering the assenblies as UNSAFE or external_Access.

HTH, Jens Suessmeyer.

http://www.sqlserver2005.de

|||

As Jens says, the most likely issue is that you haven't created the assembly with a permission set that allows you to do web "stuff". For that to work you need to create the assembly with a permission set of EXTERNAL_ACCESS.

Niels

No comments:

Post a Comment