Aspose.Words .Net - no support in Visual Studio for publishing with option ,,Produce single file"

Hi,
only with Aspose.Cells .Net I can use the option ,Produce single file" and the compiled application is running. With Aspose.Words .Net I also can activate the option field ,Produce single file", but the compiled application is running into an error. The KI results for the error messages told me, that Aspose.Words .Net doesn’t support ,Produce single file".

Is that right, or are there other ways how I can use ,Produce single file" with Aspose.Words .Net?

Bernd

@extBeniL

Can you please provide the specific error messages you are encountering when using the ‘Produce single file’ option with Aspose.Words .NET?

Hi,

the error doesn’t appear during the comulation. The error appears, when I start the compiled application, when I compiled with the option ,Produce single file".

Aspose.Words .Net is NOT licensed. Error Message:

System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
   at System.Reflection.RuntimeAssembly.get_CodeBase()
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   at w.d(Object d)
   at w.t(Object d)
   at w.d(MethodBase d, Boolean v)
   at w.c(B d)
   at w.N3(w d, B v)
   at w.M()
   at w.Y(Boolean d)
   at w.d(Object d)
   at w.t(Object d)
   at w.O()
   at w.d(Object d, UInt32 v)
   at w.Y(Boolean d)
   at w.d(Object[] d, Type[] v, Type[] c, Object[] t)
   at w.d(Int32 d, Type[] v, Type[] c, Boolean t)
   at w.e(w d, B v)
   at w.M()
   at w.Y(Boolean d)
   at w.d(Object d)
   at w.t(Object d)
   at w.O()
   at w.d(Object d, UInt32 v)
   at w.Y(Boolean d)
   at w.d(Object[] d, Type[] v, Type[] c, Object[] t)
   at w.d(Stream d, String v, Object[] c, Type[] t, Type[] n, Object[] B)
   at Aspose.Words.License.SetLicense(String licenseName)
   at SigningTool.Program.Main() in C:\DEV\SigningTool\Program.cs:line 54

Aspose.Words .Net and Aspose.Cells .Net have an embedded productive license key.

When I deactivate ,Produce single file" my compiled application is running also with Aspose.Words .Net.

The problem is when I’m using .Net 8 and also .Net 9 last versions. And I’m using the last Aspose.Words .Net package.

@extBeniL Could you please create a simple application that will allow us to reproduce the problem. I tested with a simple self-contained .NET 8 console application with enabled PublishSingleFile options:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
	<PublishSingleFile>true</PublishSingleFile>
	<SelfContained>true</SelfContained>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Aspose.Words" Version="25.1.0" />
  </ItemGroup>

</Project>

And the following simple test code:

using Aspose.Words;

Aspose.Words.License wordsLic = new Aspose.Words.License();
wordsLic.SetLicense(@"C:\Temp\Aspose.Total.NET.lic");

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Hello, World!");
doc.Save(@"C:\Temp\out.docx");
doc.Save(@"C:\Temp\out.pdf");

The published application works fine on my side.

Hello Alexey, I’m back now. The problem doesn’t appear, when I’m loading the licence file like in your example. The problem with the Aspose.Words .Net appears, when I’m using the license file as an embedded resource, whitout copy. I need the embedded way, because that are the productive license files.

Hello Alexey, here is the code from my Program.cs file:

namespace SigningTool
{
    internal static class Program
    {
        [STAThread]
        static void Main()
        //static void Main(string[] args)
        {
loaded from a single-file bundle 
            FileInfo errorlicensefile;
            string xUSERNAME = Environment.UserName; //only UserName, without Domain
            errorlicensefile = new FileInfo("C:\\Users\\" + xUSERNAME + "\\AppData\\Roaming\\Error_Loading_Licenses.txt");
            string xerrorfile = errorlicensefile.FullName;
            if (!File.Exists(errorlicensefile.FullName))
            {
                var myFile = File.Create(errorlicensefile.FullName);
                myFile.Close();
            }
            // File.WriteAllText(errorlicensefile.FullName, "------------------------------------------------");
            // #################################################################################################
            // Loading Aspose licenses
            // #################################################################################################
            string message = " ";
            string title = " ";
            //var resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            //foreach (var name in resourceNames)
            //{
            //    // Console.WriteLine(name);
            //    message = "Resourcename: " + name + "\r\n\r\n" + message;
            //    MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            //}
            Aspose.Words.License licenseWord = new Aspose.Words.License();
            // Method A (does not work)
            // Load license information from embedded resources.
            // After a possible exchange of the file under Resources, it must be set to embedded and not copy.
            //string resourceName = "Aspose.Words.NET.lic";
            //using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
            //{
            //    if (stream != null)
            //    {
            //        licenseWord.SetLicense(stream);
            //        message = "Aspose.Words .Net is licensed";
            //        // Console.WriteLine(message);
            //    }
            //    else
            //    {
            //        message = "Aspose.Words .Net is NOT licensed";
            //        // Console.WriteLine(message);
            //    }
            //    MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            //}
            // Method B
            // Load license information from embedded resources.
            // After a possible exchange of the file under Resources, it must be set to embedded and not copy.
            try
            {
                licenseWord.SetLicense("SigningTool.Resources.Aspose.Words.NET.lic");
                // message = "Aspose.Words .Net is licensed";
                // MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
            catch (Exception ex)
            {
                message = "Aspose.Words .Net is NOT licensed.\r\n\r\nThe error message is saved in the following file:\r\n\r\n" + errorlicensefile + "\r\n\r\n" + ex;
                File.WriteAllText(errorlicensefile.FullName, message);
                MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
            // Method C
            // Alternative variant for the test phase, which loads the license file from the local directory.
            // Do not use for productive license.
            //try
            //{
            //    string licenseFileNameWord = Path.Combine("C:\\Program Files\\SigningTool\\li\\", "Aspose.Words.NET.lic");
            //    licenseWord.SetLicense(licenseFileNameWord);
            //    message = "Aspose.Words .Net is licensed";
            //    MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            //}
            //catch (Exception ex)
            //{
            //    message = "Aspose.Words .Net is NOT licensed. Error Message:\r\n\r\n" + ex;
            //    MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            //}
            // #################################################################################################
            Aspose.Cells.License licenseCells = new Aspose.Cells.License();
            // Method A (does not work)
            // Load license information from embedded resources.
            // After a possible exchange of the file under Resources, it must be set to embedded and not copy.
            //string resourceName = "Aspose.Cells.NET.lic";
            //using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
            //{
            //    if (stream != null)
            //    {
            //        licenseCells.SetLicense(stream);
            //        message = "Aspose.Cells .Net is licensed";
            //        // Console.WriteLine(message);
            //    }
            //    else
            //    {
            //        message = "Aspose.Cells .Net is NOT licensed";
            //        // Console.WriteLine(message);
            //    }
            //    MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            //}
            // Method B
            // Load license information from embedded resources.
            // After a possible exchange of the file under Resources, it must be set to embedded and not copy.
            try
            {
                licenseCells.SetLicense("SigningTool.Resources.Aspose.Cells.NET.lic");
                // message = "Aspose.Cells .Net is licensed";
                // MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
            catch (Exception ex)
            {
                message = "Aspose.Cells .Net is NOT licensed.\r\n\r\nThe error message is saved in the following file:\r\n\r\n" + errorlicensefile + "\r\n\r\n" + ex;
                File.WriteAllText(errorlicensefile.FullName, message);
                MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
            // Another method to check if it's licensed.
            //Workbook workbook = new Workbook();
            //Console.WriteLine("Aspose.Cells licensed: " + workbook.IsLicensed);
            //if (workbook.IsLicensed)
            //{
            //    message = "Aspose.Cells is licensed";
            //}
            //else
            //{
            //    message = "Aspose.Cells is NOT licensed";
            //}
            //MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            // #################################################################################################
            // #################################################################################################
            ApplicationConfiguration.Initialize();
            Application.Run(new Form1());
        }
    }
}

Hello Alexey, I’ve tested again, but the error still appears, see part B).

Part A) Profil

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <PublishDir>bin\Release\net9.0-windows\publish\win-x64\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <_TargetId>Folder</_TargetId>
    <TargetFramework>net9.0-windows</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishReadyToRun>false</PublishReadyToRun>
  </PropertyGroup>
</Project>

Part b) Error when I’m starting the application:

Aspose.Words .Net is NOT licensed.

The error message is saved in the following file:

C:\Users\Bernd\AppData\Roaming\Error_Loading_Licenses.txt

System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
   at System.Reflection.RuntimeAssembly.get_CodeBase()
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   at w.d(Object d)
   at w.t(Object d)
   at w.d(MethodBase d, Boolean v)
   at w.c(B d)
   at w.N3(w d, B v)
   at w.M()
   at w.Y(Boolean d)
   at w.d(Object d)
   at w.t(Object d)
   at w.O()
   at w.d(Object d, UInt32 v)
   at w.Y(Boolean d)
   at w.d(Object[] d, Type[] v, Type[] c, Object[] t)
   at w.d(Int32 d, Type[] v, Type[] c, Boolean t)
   at w.e(w d, B v)
   at w.M()
   at w.Y(Boolean d)
   at w.d(Object d)
   at w.t(Object d)
   at w.O()
   at w.d(Object d, UInt32 v)
   at w.Y(Boolean d)
   at w.d(Object[] d, Type[] v, Type[] c, Object[] t)
   at w.d(Stream d, String v, Object[] c, Type[] t, Type[] n, Object[] B)
   at Aspose.Words.License.SetLicense(String licenseName)
   at SigningTool.Program.Main() in C:\DEV\SigningTool\Program.cs:line 72

Again part A) with // before each line, that you can see all lines.

<? xml version = "1.0" encoding = "utf-8" ?>
<!--
https ://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
  <PropertyGroup>
    <Configuration> Release </Configuration>
    <Platform > Any CPU </Platform>
    <PublishDir > bin\Release\net9.0-windows\publish\win-x64\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <_TargetId>Folder</_TargetId>
    <TargetFramework>net9.0-windows</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishReadyToRun>false</PublishReadyToRun>
  </PropertyGroup>
</Project>

And here is my csproj file, where I added true and enable from your example. Because of PublishSingleFile = true, the probleme appears with the compiled application.

WinExe net9.0-windows enable true enable true true enable 10 2.0.0.3 2.0.0.3 Symbol.ico ********************** © 2025 2.0.0.3 SigningTool for Signing and Conversion ***********

@extBeniL Please try setting the license from stream, like this:

Aspose.Words.License wordsLic = new Aspose.Words.License();

Assembly assembly = Assembly.GetExecutingAssembly();
using (Stream licenseStream = assembly.GetManifestResourceStream(@"TestOneFile.Aspose.Words.NET.lic"))
    wordsLic.SetLicense(licenseStream);

Hello Alexey, great this way is working with the embedded license file for Aspose.Words .Net.
For me the problem is solved now.

best regards
Bernd

1 Like

Only for information, if somebody is searching for an example.

Program.cs

using System.Reflection;

namespace SigningTool
{
    internal static class Program
    {
        [STAThread]
        static void Main()
        {
            FileInfo errorlicensefile;
            string xUSERNAME = Environment.UserName; //only UserName, without Domain
            errorlicensefile = new FileInfo("C:\\Users\\" + xUSERNAME + "\\AppData\\Roaming\\Error_Loading_Licenses.txt");
            string xerrorfile = errorlicensefile.FullName;
            if (!File.Exists(errorlicensefile.FullName))
            {
                var myFile = File.Create(errorlicensefile.FullName);
                myFile.Close();
            }
            // File.WriteAllText(errorlicensefile.FullName, "------------------------------------------------");
            // #################################################################################################
            // Loading Aspose licenses
            // #################################################################################################
            string message = " ";
            string title = " ";
            // #################################################################################################
            // Check the Resource Names:
            // var resourceNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            // foreach (var name in resourceNames)
            // {
            //    // Console.WriteLine(name);
            //    message = "Resourcename: " + name + "\r\n\r\n" + message;
            //    MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            // }
            // #################################################################################################
            Aspose.Words.License licenseWord = new Aspose.Words.License();
            // Load license information from embedded resources.
            // After a possible exchange of the file under Resources, it must be set to embedded and not copy.
            try
            {
                // ##########################################################################################################################
                // Variant A - returns errors when starting the compiled application if the <Produce single file> option is activated.
                // licenseWord.SetLicense("SigningTool.Resources.Aspose.Words.NET.lic");
                // ##########################################################################################################################
                // Variant B - also works with the compiled application if the <Produce single file> option is activated.
                Assembly assemblyWords = Assembly.GetExecutingAssembly();
                using (Stream licenseStreamWords = assemblyWords.GetManifestResourceStream(@"SigningTool.Resources.Aspose.Words.NET.lic"))
                licenseWord.SetLicense(licenseStreamWords);
                // ##########################################################################################################################
                // message = "Aspose.Words .Net is licensed";
                // MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
            catch (Exception ex)
            {
                message = "Aspose.Words .Net is NOT licensed.\r\n\r\nThe error message is saved in the following file:\r\n\r\n" + errorlicensefile + "\r\n\r\n" + ex;
                File.WriteAllText(errorlicensefile.FullName, message);
                MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
            // Method for development with test license files:
            // Alternative variant for the test phase, which loads the license file from the local directory.
            // Do not use for productive license.
            // try
            // {
            //    string licenseFileNameWord = Path.Combine("C:\\Program Files\\SigningTool\\li\\", "Aspose.Words.NET.lic");
            //    licenseWord.SetLicense(licenseFileNameWord);
            //    message = "Aspose.Words .Net is licensed";
            //    MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            // }
            // catch (Exception ex)
            // {
            //    message = "Aspose.Words .Net is NOT licensed. Error Message:\r\n\r\n" + ex;
            //    MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            // }
            // #################################################################################################
            Aspose.Cells.License licenseCells = new Aspose.Cells.License();
            // Load license information from embedded resources.
            // After a possible exchange of the file under Resources, it must be set to embedded and not copy.
            try
            {
                
                // ##########################################################################################################################
                // Variante A - funktioniert und liefert KEINEN Fehler beim Start der kompilierten Applikation, wenn die Option 
                // <Produce single file> aktiviert ist.
                // licenseCells.SetLicense("SigningTool.Resources.Aspose.Cells.NET.lic");
                // ##########################################################################################################################
                // Variante B - funktioniert ebenfalls bei der kompilierten Applikation, wenn die Option <Produce single file> aktiviert ist.
                Assembly assemblyCells = Assembly.GetExecutingAssembly();
                using (Stream licenseStreamCells = assemblyCells.GetManifestResourceStream(@"SigningTool.Resources.Aspose.Cells.NET.lic"))
                licenseCells.SetLicense(licenseStreamCells);
                // ##########################################################################################################################
                // message = "Aspose.Cells .Net is licensed";
                // MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
            catch (Exception ex)
            {
                message = "Aspose.Cells .Net is NOT licensed.\r\n\r\nThe error message is saved in the following file:\r\n\r\n" + errorlicensefile + "\r\n\r\n" + ex;
                File.WriteAllText(errorlicensefile.FullName, message);
                MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            }
            // Another method to check if it's licensed.
            // Workbook workbook = new Workbook();
            // Console.WriteLine("Aspose.Cells licensed: " + workbook.IsLicensed);
            // if (workbook.IsLicensed)
            // {
            //    message = "Aspose.Cells is licensed";
            // }
            // else
            // {
            //    message = "Aspose.Cells is NOT licensed";
            // }
            // MessageBox.Show(message, title, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
            // #################################################################################################
            // #################################################################################################
            ApplicationConfiguration.Initialize();
            Application.Run(new Form1());
        }
    }
}

@extBeniL Thank you for sharing your code.