SoapFormatter only works in Full Trust?

G

Guest

Hi there.

I posted an earlier issue under the name "That assembly does not allow
partially trusted callers" but have now identified what the issue is.

As explained before I am working in ASP.Net 2.0 using VB.Net and had
developed an application that ran correctly on my development server but
would not run on the shared hosting paltform which I use. The shared hosting
runs in medium trust and produced the error "That assembly does not allow
partially trusted callers"

Having played around a fair bit I have now (I think) narrowed this down to
the SoapFormatter class which I use to serialize and desiralize a class.

Code as follows:

Private Sub SerializeClass(ByVal _exRate As exRate, ByVal Path As String)

Try
Dim sf As New SoapFormatter
Dim fs As New FileStream(Path, FileMode.Create)

sf.Serialize(fs, _exRate)
fs.Close()

Catch ex As Exception
Dim myNotification As New Notification
myNotification.Subject = "Serialization Failed"
myNotification.Message = "Serialization Failed" & ex.ToString
myNotification.SendNotification(myNotification)
End Try

End Sub

and

Private Function DeserializeClass(ByVal Path As String) As exRate

Dim myExRate As New exRate

Try
Dim fs As New FileStream(Path, FileMode.Open)
Dim sf As New SoapFormatter

myExRate = CType(sf.Deserialize(fs), exRate)
fs.Close()
Catch ex As Exception
End Try

Return myExRate

End Function

I now understand that the SoapFormatter class only works in full trust. Is
there a work around for this issue or perhaps a better way to do the above.

I was basically trying to save the values for the properties of the class to
be used at a later date.
 
A

Alvin Bruney [MVP]

I couldn't find where the soapformatter class requires full trust, granted I
didn't look long and hard either.

If that's the case, you will need to use CAS policy to remove that
restriction. Use the configuration utility to configure the appropriate
policy on the assembly. Ideally, you would grant the assembly full trust
(not recommended) or some combination of permissions to allow it run that is
less than full trust.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
 
G

Guest

Alvin

Thanks for your response.

If you look at the following MSDN article:

http://msdn2.microsoft.com/en-us/library/chyky9hf(VS.80).aspx

There is a section at the bottom that seems to indicate that the serialise
method requires Full Trust.

--------------------------------------------------------------------
..NET Framework Security

Full trust for the immediate caller. This member cannot be used by partially
trusted code. For more information, see .

---------------------------------------------------------------------

I assume where you are reffering to the CAS policy you are reffering to
editing the machine.config file and adding a line that basically says the
equivalent of grant SoapFormatter full trust but nothing else?

--
Regards

Martyn Fewtrell
 
A

Alvin Bruney [MVP]

Yes i was, its rather easy to create a cas policy and push it to the prod.
environment. That will fix you up.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc
Professional VSTO 2005 - Wrox/Wiley
 
W

Walter Wang [MSFT]

Hi Martyn,

As you have already knew, the SoapFormatter.Serialize requires immediate
caller must have full trust. There're several approaches for such scenario,
please see following article for more information:

#How To: Use Code Access Security in ASP.NET 2.0
http://msdn2.microsoft.com/en-us/library/ms998326.aspx


For example, one possible approach is to create a wrapper assembly,
configure this wrapper assembly to run with full trust, and apply
AllowPartialTrustedCallersAttribute to this wrapper assembly; then you can
call this wrapper assembly from your web application.


Hope this helps. Please feel free to let me know if there's anything
unclear. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Martyn,

Please feel free to let us know if the issue is still not resolved. Thanks.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top