BlazorWorker icon indicating copy to clipboard operation
BlazorWorker copied to clipboard

Not able to pass a third party type to function for Blazorworker

Open bpyzik opened this issue 3 years ago • 3 comments

I am running into the issue of passing a file type into the function in runasync as shown here:

var service = await worker.CreateBackgroundServiceAsync<ExcelService>(
			  options => options
			  .AddConventionalAssemblyOfService()
			  .AddAssemblies("BlazorInputFile.dll")
			  .AddAssemblies("DocumentFormat.OpenXml.dll")
			  .AddAssemblies("DocumentFormat.OpenXml.Packaging.dll")
			  .AddAssemblies("DocumentFormat.OpenXml.Spreadsheet.dll")
			  .AddAssemblies("System.IO.Packaging.dll")
			  .AddAssemblies("System.Xml.Linq.dll")
			);
var result = await service.RunAsync(s => s.GetLinesFromSheet(files));

and I recieve error:

System.Runtime.Serialization.SerializationException: Error converting type: Type 'BlazorInputFile.FileListEntryImpl[]' with data contract name 'ArrayOfFileListEntryImpl:http://schemas.datacontract.org/2004/07/BlazorInputFile' is not expected.`

files is of type IFileEntry[] from the BlazorInputFile package. Is there a way to let the serviceworker know how to serialize this?

bpyzik avatar Oct 30 '20 20:10 bpyzik

Maybe, but far from simple.

The serialization of the expression depends on the default expression serializer: https://github.com/Tewr/BlazorWorker/blob/master/src/BlazorWorker.WorkerBackgroundService/SerializeLinqExpressionSerializer.cs

There is no simple way of replacing this serializer with you own implementation, nor configuring serialization for a specific type... I had plans to make it configurable somehow, however the intention has never become practice. It's difficult to design a simple API around this stuff and also pass it over the process border.

Id suggest serializing any unsupported type by yourself and pass it over using a byte array or a string.

Tewr avatar Nov 01 '20 09:11 Tewr

@bpyzik Do you mean IFileListEntry ? https://github.com/SteveSandersonMS/BlazorInputFile/blob/master/BlazorInputFile/IFileListEntry.cs

That interface has a stream reference. Streams are, by their nature, not serializable. There is an issue open for supporting streams though.

Tewr avatar Nov 07 '20 06:11 Tewr

Id suggest serializing any unsupported type by yourself and pass it over using a byte array or a string.

Scrolling issues during the implementation phase and found this comment. Very useful tip as I was having issues passing semi-complex objects to the workers. We have our own serialization to BSON (Binary json) configured so I decided to just use that manually on the main thread and worker side for serialization and deserialization. I tried going down the rabbit hole of getting the built in serialization to work with our complex Data Model but it ended up getting harder and harder and eventually started messing with our own serialization functions...

Matheos96 avatar Feb 13 '24 21:02 Matheos96

Closed by #98

Tewr avatar Feb 28 '24 17:02 Tewr