An exception when use Remoting and Windows Service

An exception when use Remoting and Windows Service

Old forum URL: forums.lhotka.net/forums/t/516.aspx


phucphlq posted on Friday, June 30, 2006

I catch an exception, I use Windows Service, CSLA 2.0.2, Remoting Portal. How I do? Please help me!

Because of security restrictions, the type Csla.Server.DataPortalContext cannot be accessed.

System.Runtime.Serialization.SerializationException was unhandled
  Message="Because of security restrictions, the type Csla.Server.DataPortalContext cannot be accessed."
  Source="mscorlib"
  StackTrace:
    Server stack trace:
       at System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject(Type type)
       at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObject(ParseRecord pr)
       at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr)
       at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
       at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
       at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
       at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
       at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
       at System.Runtime.Remoting.Channels.CoreChannel.DeserializeBinaryRequestMessage(String objectUri, Stream inputStream, Boolean bStrictBinding, TypeFilterLevel securityLevel)
       at System.Runtime.Remoting.Channels.BinaryServerFormatterSink.ProcessMessage(IServerChannelSinkStack sinkStack, IMessage requestMsg, ITransportHeaders requestHeaders, Stream requestStream, IMessage& responseMsg, ITransportHeaders& responseHeaders, Stream& responseStream)
    Exception rethrown at [0]:
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at Csla.Server.IDataPortalServer.Fetch(Type objectType, Object criteria, DataPortalContext context)
       at Csla.DataPortalClient.RemotingProxy.Fetch(Type objectType, Object criteria, DataPortalContext context) in D:\PHLQP\RD\csla20cs\csla20cs\Csla\DataPortal\Client\RemotingProxy.cs:line 111
       at Csla.DataPortal.Fetch(Type objectType, Object criteria) in D:\PHLQP\RD\csla20cs\csla20cs\Csla\DataPortal\Client\DataPortal.cs:line 184
       at Csla.DataPortal.Fetch[T](Object criteria) in D:\PHLQP\RD\csla20cs\csla20cs\Csla\DataPortal\Client\DataPortal.cs:line 138
       at BusinessObjects.BasicObjectList.GetEditableRootList(Int32 id) in D:\PHLQP\Softech\BusinessObjects\BasicObjectList.cs:line 40
       at Softech.Form1.button5_Click(Object sender, EventArgs e) in D:\PHLQP\Softech\Softech\Form1.cs:line 106
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Softech.Program.Main() in D:\PHLQP\Softech\Softech\Program.cs:line 21
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Thanks.

Massong replied on Friday, June 30, 2006

Hi,

We use a Windows Service for remoting, too. In the OnStart-method of our service we configure remoting this way:

RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off

Dim properties As New Hashtable

properties("name") = "MyChannel"
properties("port") = TcpPort            ' TpcPort goes here

Dim serverFormatter As New BinaryServerFormatterSinkProvider
serverFormatter.TypeFilterLevel = Runtime.Serialization.Formatters.TypeFilterLevel.Full

Dim channel As New TcpServerChannel(properties, serverFormatter)
ChannelServices.RegisterChannel(channel)

RemotingConfiguration.RegisterWellKnownServiceType(GetType(Csla.Server.Hosts.RemotingPortal), "MyService", WellKnownObjectMode.SingleCall)

Hope this helps,
Christian

phucphlq replied on Friday, June 30, 2006

Thanks for your rely!

I used your method and my program has not raise an exception. But it suspend and not return anythingSad [:(]

Could you explain for me more detail

Massong replied on Wednesday, July 05, 2006

I created a Windows Service project and added a reference to the Csla.dll and to my business dll. Then I overrode the OnStart-method of the Windows Service:

Protected Overrides Sub OnStart(ByVal args() As String)
   RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off

   Dim properties As New Hashtable
   properties("name") = "MyChannel"
   properties("port") = 54321

   Dim serverFormatter As New BinaryServerFormatterSinkProvider
   serverFormatter.TypeFilterLevel = Runtime.Serialization.Formatters.TypeFilterLevel.Full

   Dim channel As New TcpServerChannel(properties, serverFormatter)
   ChannelServices.RegisterChannel(channel)

   RemotingConfiguration.RegisterWellKnownServiceType(GetType(Csla.Server.Hosts.RemotingPortal), "MyService", WellKnownObjectMode.SingleCall)
End Sub

Of course you should use a better channel name than “MyChannel” and a better service name than “MyService”, shouldn't use this port number and should write some code for error handling.

I installed the service with the install utility:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\InstallUtil RemoteService.exe

and

NET START "MyService"

My client application’s config file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key ="CslaAuthentication" value ="Csla"/>
    <add key ="CslaDataPortalProxy" value ="Csla.DataPortalClient.RemotingProxy, Csla" />
    <add key ="CslaDataPortalUrl" value ="tcp://servername:54321/MyService" />
  </appSettings>
</configuration>

Please make sure that you use the same tcp port number and service name on service side and application side.

Greetings,
Christian

phucphlq replied on Sunday, July 09, 2006

Thanks Massong for your rely.

I had code, but have an error at

return portal.Create(objectType, criteria, context);

Cannot evaluate expression because a native frame is on top of the call stack.

Massong replied on Monday, July 10, 2006

Hi phucphlq,

This doesn’t seem to be a problem with the Windows Service. The call of the Create-method is a little bit strange to me. Your business object’s factory method should look like this (see page 78 of Rocky’s VB-Book):

Public Shared Function NewEmployee(context as Context) As Employee
  Return DataPortal.Create(Of Employee)(New Criteria(context))
End Function

And your DataPortal_Create method should look like this (page 425):

Private Overloads Sub DataPortal_Create(ByVal criteria As Criteria)
  ...
End Sub

For testing you could try to run the ProjectTracker example in a Windows Service.

jrnail23 replied on Monday, July 24, 2006

Christian,

It looks like this may be just what we needed... thanks a bunch!

James

phucphlq replied on Monday, April 16, 2007

Hi all!

How to config in App.config and not write code at Service.

Thanks

Copyright (c) Marimer LLC